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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/wait.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
33 #include "vmbus_private.h"
36 struct vmbus_connection vmbus_connection
= {
37 .conn_state
= DISCONNECTED
,
38 .next_gpadl_handle
= ATOMIC_INIT(0xE1E10),
42 * vmbus_connect - Sends a connect request on the partition service connection
44 int vmbus_connect(void)
47 struct vmbus_channel_msginfo
*msginfo
= NULL
;
48 struct vmbus_channel_initiate_contact
*msg
;
51 /* Make sure we are not connecting or connected */
52 if (vmbus_connection
.conn_state
!= DISCONNECTED
)
55 /* Initialize the vmbus connection */
56 vmbus_connection
.conn_state
= CONNECTING
;
57 vmbus_connection
.work_queue
= create_workqueue("hv_vmbus_con");
58 if (!vmbus_connection
.work_queue
) {
63 INIT_LIST_HEAD(&vmbus_connection
.chn_msg_list
);
64 spin_lock_init(&vmbus_connection
.channelmsg_lock
);
66 INIT_LIST_HEAD(&vmbus_connection
.chn_list
);
67 spin_lock_init(&vmbus_connection
.channel_lock
);
70 * Setup the vmbus event connection for channel interrupt
73 vmbus_connection
.int_page
=
74 (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
, 0);
75 if (vmbus_connection
.int_page
== NULL
) {
80 vmbus_connection
.recv_int_page
= vmbus_connection
.int_page
;
81 vmbus_connection
.send_int_page
=
82 (void *)((unsigned long)vmbus_connection
.int_page
+
86 * Setup the monitor notification facility. The 1st page for
87 * parent->child and the 2nd page for child->parent
89 vmbus_connection
.monitor_pages
=
90 (void *)__get_free_pages((GFP_KERNEL
|__GFP_ZERO
), 1);
91 if (vmbus_connection
.monitor_pages
== NULL
) {
96 msginfo
= kzalloc(sizeof(*msginfo
) +
97 sizeof(struct vmbus_channel_initiate_contact
),
99 if (msginfo
== NULL
) {
104 init_waitqueue_head(&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 ret
= vmbus_post_msg(msg
,
127 sizeof(struct vmbus_channel_initiate_contact
));
129 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
130 list_del(&msginfo
->msglistentry
);
131 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
,
136 /* Wait for the connection response */
137 msginfo
->wait_condition
= 0;
138 wait_event_timeout(msginfo
->waitevent
, msginfo
->wait_condition
,
139 msecs_to_jiffies(1000));
140 if (msginfo
->wait_condition
== 0) {
141 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
,
143 list_del(&msginfo
->msglistentry
);
144 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
,
150 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
151 list_del(&msginfo
->msglistentry
);
152 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
154 /* Check if successful */
155 if (msginfo
->response
.version_response
.version_supported
) {
156 vmbus_connection
.conn_state
= CONNECTED
;
158 pr_err("Unable to connect, "
159 "Version %d not supported by Hyper-V\n",
160 VMBUS_REVISION_NUMBER
);
169 vmbus_connection
.conn_state
= DISCONNECTED
;
171 if (vmbus_connection
.work_queue
)
172 destroy_workqueue(vmbus_connection
.work_queue
);
174 if (vmbus_connection
.int_page
) {
175 free_pages((unsigned long)vmbus_connection
.int_page
, 0);
176 vmbus_connection
.int_page
= NULL
;
179 if (vmbus_connection
.monitor_pages
) {
180 free_pages((unsigned long)vmbus_connection
.monitor_pages
, 1);
181 vmbus_connection
.monitor_pages
= NULL
;
191 * Sends a disconnect request on the partition service connection
193 int vmbus_disconnect(void)
196 struct vmbus_channel_message_header
*msg
;
198 /* Make sure we are connected */
199 if (vmbus_connection
.conn_state
!= CONNECTED
)
202 msg
= kzalloc(sizeof(struct vmbus_channel_message_header
), GFP_KERNEL
);
206 msg
->msgtype
= CHANNELMSG_UNLOAD
;
208 ret
= vmbus_post_msg(msg
,
209 sizeof(struct vmbus_channel_message_header
));
213 free_pages((unsigned long)vmbus_connection
.int_page
, 0);
214 free_pages((unsigned long)vmbus_connection
.monitor_pages
, 1);
216 /* TODO: iterate thru the msg list and free up */
217 destroy_workqueue(vmbus_connection
.work_queue
);
219 vmbus_connection
.conn_state
= DISCONNECTED
;
221 pr_info("hv_vmbus disconnected\n");
229 * relid2channel - Get the channel object given its
230 * child relative id (ie channel id)
232 struct vmbus_channel
*relid2channel(u32 relid
)
234 struct vmbus_channel
*channel
;
235 struct vmbus_channel
*found_channel
= NULL
;
238 spin_lock_irqsave(&vmbus_connection
.channel_lock
, flags
);
239 list_for_each_entry(channel
, &vmbus_connection
.chn_list
, listentry
) {
240 if (channel
->offermsg
.child_relid
== relid
) {
241 found_channel
= channel
;
245 spin_unlock_irqrestore(&vmbus_connection
.channel_lock
, flags
);
247 return found_channel
;
251 * process_chn_event - Process a channel event notification
253 static void process_chn_event(u32 relid
)
255 struct vmbus_channel
*channel
;
257 /* ASSERT(relId > 0); */
260 * Find the channel based on this relid and invokes the
261 * channel callback to process the event
263 channel
= relid2channel(relid
);
266 vmbus_onchannel_event(channel
);
268 * WorkQueueQueueWorkItem(channel->dataWorkQueue,
269 * vmbus_onchannel_event,
273 pr_err("channel not found for relid - %u\n", relid
);
278 * vmbus_on_event - Handler for events
280 void vmbus_on_event(unsigned long data
)
283 u32 maxdword
= MAX_NUM_CHANNELS_SUPPORTED
>> 5;
286 u32
*recv_int_page
= vmbus_connection
.recv_int_page
;
291 for (dword
= 0; dword
< maxdword
; dword
++) {
292 if (!recv_int_page
[dword
])
294 for (bit
= 0; bit
< 32; bit
++) {
295 if (sync_test_and_clear_bit(bit
, (unsigned long *)&recv_int_page
[dword
])) {
296 relid
= (dword
<< 5) + bit
;
299 /* special case - vmbus channel protocol msg */
302 process_chn_event(relid
);
309 * vmbus_post_msg - Send a msg on the vmbus's message connection
311 int vmbus_post_msg(void *buffer
, size_t buflen
)
313 union hv_connection_id conn_id
;
316 conn_id
.u
.id
= VMBUS_MESSAGE_CONNECTION_ID
;
317 return hv_post_message(conn_id
, 1, buffer
, buflen
);
321 * vmbus_set_event - Send an event notification to the parent
323 int vmbus_set_event(u32 child_relid
)
325 /* Each u32 represents 32 channels */
326 sync_set_bit(child_relid
& 31,
327 (unsigned long *)vmbus_connection
.send_int_page
+
330 return hv_signal_event();