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>
28 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/vmalloc.h>
34 #include "hyperv_vmbus.h"
37 struct vmbus_connection vmbus_connection
= {
38 .conn_state
= DISCONNECTED
,
39 .next_gpadl_handle
= ATOMIC_INIT(0xE1E10),
43 * vmbus_connect - Sends a connect request on the partition service connection
45 int vmbus_connect(void)
49 struct vmbus_channel_msginfo
*msginfo
= NULL
;
50 struct vmbus_channel_initiate_contact
*msg
;
53 /* Make sure we are not connecting or connected */
54 if (vmbus_connection
.conn_state
!= DISCONNECTED
)
57 /* Initialize the vmbus connection */
58 vmbus_connection
.conn_state
= CONNECTING
;
59 vmbus_connection
.work_queue
= create_workqueue("hv_vmbus_con");
60 if (!vmbus_connection
.work_queue
) {
65 INIT_LIST_HEAD(&vmbus_connection
.chn_msg_list
);
66 spin_lock_init(&vmbus_connection
.channelmsg_lock
);
68 INIT_LIST_HEAD(&vmbus_connection
.chn_list
);
69 spin_lock_init(&vmbus_connection
.channel_lock
);
72 * Setup the vmbus event connection for channel interrupt
75 vmbus_connection
.int_page
=
76 (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
, 0);
77 if (vmbus_connection
.int_page
== NULL
) {
82 vmbus_connection
.recv_int_page
= vmbus_connection
.int_page
;
83 vmbus_connection
.send_int_page
=
84 (void *)((unsigned long)vmbus_connection
.int_page
+
88 * Setup the monitor notification facility. The 1st page for
89 * parent->child and the 2nd page for child->parent
91 vmbus_connection
.monitor_pages
=
92 (void *)__get_free_pages((GFP_KERNEL
|__GFP_ZERO
), 1);
93 if (vmbus_connection
.monitor_pages
== NULL
) {
98 msginfo
= kzalloc(sizeof(*msginfo
) +
99 sizeof(struct vmbus_channel_initiate_contact
),
101 if (msginfo
== NULL
) {
106 init_completion(&msginfo
->waitevent
);
108 msg
= (struct vmbus_channel_initiate_contact
*)msginfo
->msg
;
110 msg
->header
.msgtype
= CHANNELMSG_INITIATE_CONTACT
;
111 msg
->vmbus_version_requested
= VMBUS_REVISION_NUMBER
;
112 msg
->interrupt_page
= virt_to_phys(vmbus_connection
.int_page
);
113 msg
->monitor_page1
= virt_to_phys(vmbus_connection
.monitor_pages
);
114 msg
->monitor_page2
= virt_to_phys(
115 (void *)((unsigned long)vmbus_connection
.monitor_pages
+
119 * Add to list before we send the request since we may
120 * receive the response before returning from this routine
122 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
123 list_add_tail(&msginfo
->msglistentry
,
124 &vmbus_connection
.chn_msg_list
);
126 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
128 ret
= vmbus_post_msg(msg
,
129 sizeof(struct vmbus_channel_initiate_contact
));
131 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
132 list_del(&msginfo
->msglistentry
);
133 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
,
138 /* Wait for the connection response */
139 t
= wait_for_completion_timeout(&msginfo
->waitevent
, 5*HZ
);
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 * relid2channel - Get the channel object given its
192 * child relative id (ie channel id)
194 struct vmbus_channel
*relid2channel(u32 relid
)
196 struct vmbus_channel
*channel
;
197 struct vmbus_channel
*found_channel
= NULL
;
200 spin_lock_irqsave(&vmbus_connection
.channel_lock
, flags
);
201 list_for_each_entry(channel
, &vmbus_connection
.chn_list
, listentry
) {
202 if (channel
->offermsg
.child_relid
== relid
) {
203 found_channel
= channel
;
207 spin_unlock_irqrestore(&vmbus_connection
.channel_lock
, flags
);
209 return found_channel
;
213 * process_chn_event - Process a channel event notification
215 static void process_chn_event(u32 relid
)
217 struct vmbus_channel
*channel
;
221 * Find the channel based on this relid and invokes the
222 * channel callback to process the event
224 channel
= relid2channel(relid
);
226 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
227 if (channel
&& (channel
->onchannel_callback
!= NULL
)) {
228 channel
->onchannel_callback(channel
->channel_callback_context
);
230 pr_err("channel not found for relid - %u\n", relid
);
232 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
236 * vmbus_on_event - Handler for events
238 void vmbus_on_event(unsigned long data
)
241 u32 maxdword
= MAX_NUM_CHANNELS_SUPPORTED
>> 5;
244 u32
*recv_int_page
= vmbus_connection
.recv_int_page
;
249 for (dword
= 0; dword
< maxdword
; dword
++) {
250 if (!recv_int_page
[dword
])
252 for (bit
= 0; bit
< 32; bit
++) {
253 if (sync_test_and_clear_bit(bit
, (unsigned long *)&recv_int_page
[dword
])) {
254 relid
= (dword
<< 5) + bit
;
258 * Special case - vmbus
259 * channel protocol msg
263 process_chn_event(relid
);
270 * vmbus_post_msg - Send a msg on the vmbus's message connection
272 int vmbus_post_msg(void *buffer
, size_t buflen
)
274 union hv_connection_id conn_id
;
279 conn_id
.u
.id
= VMBUS_MESSAGE_CONNECTION_ID
;
282 * hv_post_message() can have transient failures because of
283 * insufficient resources. Retry the operation a couple of
284 * times before giving up.
286 while (retries
< 3) {
287 ret
= hv_post_message(conn_id
, 1, buffer
, buflen
);
288 if (ret
!= HV_STATUS_INSUFFICIENT_BUFFERS
)
297 * vmbus_set_event - Send an event notification to the parent
299 int vmbus_set_event(u32 child_relid
)
301 /* Each u32 represents 32 channels */
302 sync_set_bit(child_relid
& 31,
303 (unsigned long *)vmbus_connection
.send_int_page
+
306 return hv_signal_event();