]>
Commit | Line | Data |
---|---|---|
3e7ee490 HJ |
1 | /* |
2 | * | |
3 | * Copyright (c) 2009, Microsoft Corporation. | |
4 | * | |
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. | |
8 | * | |
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 | |
12 | * more details. | |
13 | * | |
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. | |
17 | * | |
18 | * Authors: | |
19 | * Haiyang Zhang <haiyangz@microsoft.com> | |
20 | * Hank Janssen <hjanssen@microsoft.com> | |
21 | * | |
22 | */ | |
a0086dc5 GKH |
23 | #include <linux/kernel.h> |
24 | #include <linux/mm.h> | |
5a0e3ad6 | 25 | #include <linux/slab.h> |
a0086dc5 | 26 | #include <linux/vmalloc.h> |
4983b39a | 27 | #include "osd.h" |
645954c5 | 28 | #include "logging.h" |
72daf320 | 29 | #include "vmbus_private.h" |
3e7ee490 | 30 | |
3e7ee490 | 31 | |
15b2f647 | 32 | struct VMBUS_CONNECTION vmbus_connection = { |
3e7ee490 | 33 | .ConnectState = Disconnected, |
f4888417 | 34 | .NextGpadlHandle = ATOMIC_INIT(0xE1E10), |
3e7ee490 HJ |
35 | }; |
36 | ||
3e189519 | 37 | /* |
c6977677 | 38 | * vmbus_connect - Sends a connect request on the partition service connection |
fd8b85ea | 39 | */ |
c6977677 | 40 | int vmbus_connect(void) |
3e7ee490 | 41 | { |
fd8b85ea | 42 | int ret = 0; |
15b2f647 | 43 | struct vmbus_channel_msginfo *msginfo = NULL; |
82250213 | 44 | struct vmbus_channel_initiate_contact *msg; |
dd0813b6 | 45 | unsigned long flags; |
3e7ee490 | 46 | |
454f18a9 | 47 | /* Make sure we are not connecting or connected */ |
15b2f647 | 48 | if (vmbus_connection.ConnectState != Disconnected) |
3e7ee490 HJ |
49 | return -1; |
50 | ||
454f18a9 | 51 | /* Initialize the vmbus connection */ |
15b2f647 HZ |
52 | vmbus_connection.ConnectState = Connecting; |
53 | vmbus_connection.WorkQueue = create_workqueue("hv_vmbus_con"); | |
54 | if (!vmbus_connection.WorkQueue) { | |
de65a384 BP |
55 | ret = -1; |
56 | goto Cleanup; | |
57 | } | |
3e7ee490 | 58 | |
15b2f647 HZ |
59 | INIT_LIST_HEAD(&vmbus_connection.ChannelMsgList); |
60 | spin_lock_init(&vmbus_connection.channelmsg_lock); | |
3e7ee490 | 61 | |
15b2f647 HZ |
62 | INIT_LIST_HEAD(&vmbus_connection.ChannelList); |
63 | spin_lock_init(&vmbus_connection.channel_lock); | |
3e7ee490 | 64 | |
454f18a9 BP |
65 | /* |
66 | * Setup the vmbus event connection for channel interrupt | |
67 | * abstraction stuff | |
68 | */ | |
15b2f647 HZ |
69 | vmbus_connection.InterruptPage = osd_page_alloc(1); |
70 | if (vmbus_connection.InterruptPage == NULL) { | |
3e7ee490 HJ |
71 | ret = -1; |
72 | goto Cleanup; | |
73 | } | |
74 | ||
15b2f647 HZ |
75 | vmbus_connection.RecvInterruptPage = vmbus_connection.InterruptPage; |
76 | vmbus_connection.SendInterruptPage = | |
77 | (void *)((unsigned long)vmbus_connection.InterruptPage + | |
fd8b85ea | 78 | (PAGE_SIZE >> 1)); |
3e7ee490 | 79 | |
fd8b85ea GKH |
80 | /* |
81 | * Setup the monitor notification facility. The 1st page for | |
82 | * parent->child and the 2nd page for child->parent | |
454f18a9 | 83 | */ |
15b2f647 HZ |
84 | vmbus_connection.MonitorPages = osd_page_alloc(2); |
85 | if (vmbus_connection.MonitorPages == NULL) { | |
3e7ee490 HJ |
86 | ret = -1; |
87 | goto Cleanup; | |
88 | } | |
89 | ||
15b2f647 | 90 | msginfo = kzalloc(sizeof(*msginfo) + |
fd8b85ea GKH |
91 | sizeof(struct vmbus_channel_initiate_contact), |
92 | GFP_KERNEL); | |
15b2f647 | 93 | if (msginfo == NULL) { |
8cad0af9 | 94 | ret = -ENOMEM; |
3e7ee490 HJ |
95 | goto Cleanup; |
96 | } | |
97 | ||
15b2f647 HZ |
98 | msginfo->waitevent = osd_waitevent_create(); |
99 | if (!msginfo->waitevent) { | |
80d11b2a BP |
100 | ret = -ENOMEM; |
101 | goto Cleanup; | |
102 | } | |
103 | ||
15b2f647 | 104 | msg = (struct vmbus_channel_initiate_contact *)msginfo->msg; |
3e7ee490 | 105 | |
c50f7fb2 HZ |
106 | msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT; |
107 | msg->vmbus_version_requested = VMBUS_REVISION_NUMBER; | |
15b2f647 HZ |
108 | msg->interrupt_page = virt_to_phys(vmbus_connection.InterruptPage); |
109 | msg->monitor_page1 = virt_to_phys(vmbus_connection.MonitorPages); | |
c50f7fb2 | 110 | msg->monitor_page2 = virt_to_phys( |
15b2f647 | 111 | (void *)((unsigned long)vmbus_connection.MonitorPages + |
fd8b85ea | 112 | PAGE_SIZE)); |
3e7ee490 | 113 | |
454f18a9 BP |
114 | /* |
115 | * Add to list before we send the request since we may | |
116 | * receive the response before returning from this routine | |
117 | */ | |
15b2f647 HZ |
118 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
119 | list_add_tail(&msginfo->msglistentry, | |
120 | &vmbus_connection.ChannelMsgList); | |
53af545b | 121 | |
15b2f647 | 122 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 123 | |
fd8b85ea GKH |
124 | DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, " |
125 | "monitor1 pfn %llx,, monitor2 pfn %llx", | |
c50f7fb2 | 126 | msg->interrupt_page, msg->monitor_page1, msg->monitor_page2); |
3e7ee490 HJ |
127 | |
128 | DPRINT_DBG(VMBUS, "Sending channel initiate msg..."); | |
c6977677 | 129 | ret = vmbus_post_msg(msg, |
fd8b85ea GKH |
130 | sizeof(struct vmbus_channel_initiate_contact)); |
131 | if (ret != 0) { | |
15b2f647 | 132 | list_del(&msginfo->msglistentry); |
3e7ee490 HJ |
133 | goto Cleanup; |
134 | } | |
135 | ||
454f18a9 | 136 | /* Wait for the connection response */ |
15b2f647 | 137 | osd_waitevent_wait(msginfo->waitevent); |
3e7ee490 | 138 | |
15b2f647 | 139 | list_del(&msginfo->msglistentry); |
3e7ee490 | 140 | |
454f18a9 | 141 | /* Check if successful */ |
15b2f647 | 142 | if (msginfo->response.version_response.version_supported) { |
3e7ee490 | 143 | DPRINT_INFO(VMBUS, "Vmbus connected!!"); |
15b2f647 | 144 | vmbus_connection.ConnectState = Connected; |
3e7ee490 | 145 | |
fd8b85ea GKH |
146 | } else { |
147 | DPRINT_ERR(VMBUS, "Vmbus connection failed!!..." | |
148 | "current version (%d) not supported", | |
149 | VMBUS_REVISION_NUMBER); | |
3e7ee490 | 150 | ret = -1; |
3e7ee490 HJ |
151 | goto Cleanup; |
152 | } | |
153 | ||
15b2f647 HZ |
154 | kfree(msginfo->waitevent); |
155 | kfree(msginfo); | |
3e7ee490 HJ |
156 | return 0; |
157 | ||
158 | Cleanup: | |
15b2f647 | 159 | vmbus_connection.ConnectState = Disconnected; |
3e7ee490 | 160 | |
15b2f647 HZ |
161 | if (vmbus_connection.WorkQueue) |
162 | destroy_workqueue(vmbus_connection.WorkQueue); | |
3e7ee490 | 163 | |
15b2f647 HZ |
164 | if (vmbus_connection.InterruptPage) { |
165 | osd_page_free(vmbus_connection.InterruptPage, 1); | |
166 | vmbus_connection.InterruptPage = NULL; | |
3e7ee490 HJ |
167 | } |
168 | ||
15b2f647 HZ |
169 | if (vmbus_connection.MonitorPages) { |
170 | osd_page_free(vmbus_connection.MonitorPages, 2); | |
171 | vmbus_connection.MonitorPages = NULL; | |
3e7ee490 HJ |
172 | } |
173 | ||
15b2f647 HZ |
174 | if (msginfo) { |
175 | kfree(msginfo->waitevent); | |
176 | kfree(msginfo); | |
3e7ee490 HJ |
177 | } |
178 | ||
3e7ee490 HJ |
179 | return ret; |
180 | } | |
181 | ||
3e189519 | 182 | /* |
c6977677 HZ |
183 | * vmbus_disconnect - |
184 | * Sends a disconnect request on the partition service connection | |
fd8b85ea | 185 | */ |
c6977677 | 186 | int vmbus_disconnect(void) |
3e7ee490 | 187 | { |
fd8b85ea | 188 | int ret = 0; |
82250213 | 189 | struct vmbus_channel_message_header *msg; |
3e7ee490 | 190 | |
454f18a9 | 191 | /* Make sure we are connected */ |
15b2f647 | 192 | if (vmbus_connection.ConnectState != Connected) |
3e7ee490 HJ |
193 | return -1; |
194 | ||
82250213 | 195 | msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL); |
8cad0af9 BP |
196 | if (!msg) |
197 | return -ENOMEM; | |
3e7ee490 | 198 | |
c50f7fb2 | 199 | msg->msgtype = CHANNELMSG_UNLOAD; |
3e7ee490 | 200 | |
c6977677 | 201 | ret = vmbus_post_msg(msg, |
fd8b85ea | 202 | sizeof(struct vmbus_channel_message_header)); |
3e7ee490 | 203 | if (ret != 0) |
3e7ee490 | 204 | goto Cleanup; |
3e7ee490 | 205 | |
15b2f647 | 206 | osd_page_free(vmbus_connection.InterruptPage, 1); |
3e7ee490 | 207 | |
454f18a9 | 208 | /* TODO: iterate thru the msg list and free up */ |
15b2f647 | 209 | destroy_workqueue(vmbus_connection.WorkQueue); |
3e7ee490 | 210 | |
15b2f647 | 211 | vmbus_connection.ConnectState = Disconnected; |
3e7ee490 HJ |
212 | |
213 | DPRINT_INFO(VMBUS, "Vmbus disconnected!!"); | |
214 | ||
215 | Cleanup: | |
fd8b85ea | 216 | kfree(msg); |
3e7ee490 HJ |
217 | return ret; |
218 | } | |
219 | ||
3e189519 | 220 | /* |
c6977677 HZ |
221 | * relid2channel - Get the channel object given its |
222 | * child relative id (ie channel id) | |
fd8b85ea | 223 | */ |
c6977677 | 224 | struct vmbus_channel *relid2channel(u32 relid) |
3e7ee490 | 225 | { |
aded7165 | 226 | struct vmbus_channel *channel; |
15b2f647 | 227 | struct vmbus_channel *found_channel = NULL; |
0f5e44ca | 228 | unsigned long flags; |
3e7ee490 | 229 | |
15b2f647 HZ |
230 | spin_lock_irqsave(&vmbus_connection.channel_lock, flags); |
231 | list_for_each_entry(channel, &vmbus_connection.ChannelList, listentry) { | |
232 | if (channel->offermsg.child_relid == relid) { | |
233 | found_channel = channel; | |
3e7ee490 HJ |
234 | break; |
235 | } | |
236 | } | |
15b2f647 | 237 | spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); |
3e7ee490 | 238 | |
15b2f647 | 239 | return found_channel; |
3e7ee490 HJ |
240 | } |
241 | ||
3e189519 | 242 | /* |
c6977677 | 243 | * process_chn_event - Process a channel event notification |
fd8b85ea | 244 | */ |
c6977677 | 245 | static void process_chn_event(void *context) |
3e7ee490 | 246 | { |
aded7165 | 247 | struct vmbus_channel *channel; |
15b2f647 | 248 | u32 relid = (u32)(unsigned long)context; |
3e7ee490 | 249 | |
972b9529 | 250 | /* ASSERT(relId > 0); */ |
3e7ee490 | 251 | |
454f18a9 BP |
252 | /* |
253 | * Find the channel based on this relid and invokes the | |
254 | * channel callback to process the event | |
255 | */ | |
c6977677 | 256 | channel = relid2channel(relid); |
3e7ee490 | 257 | |
fd8b85ea | 258 | if (channel) { |
fff41b2e | 259 | vmbus_onchannel_event(channel); |
fd8b85ea GKH |
260 | /* |
261 | * WorkQueueQueueWorkItem(channel->dataWorkQueue, | |
fff41b2e | 262 | * vmbus_onchannel_event, |
0686e4f4 | 263 | * (void*)channel); |
fd8b85ea GKH |
264 | */ |
265 | } else { | |
15b2f647 | 266 | DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relid); |
3e7ee490 HJ |
267 | } |
268 | } | |
269 | ||
3e189519 | 270 | /* |
c6977677 | 271 | * vmbus_on_event - Handler for events |
fd8b85ea | 272 | */ |
c6977677 | 273 | void vmbus_on_event(void) |
3e7ee490 HJ |
274 | { |
275 | int dword; | |
3e7ee490 HJ |
276 | int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5; |
277 | int bit; | |
278 | int relid; | |
15b2f647 | 279 | u32 *recv_int_page = vmbus_connection.RecvInterruptPage; |
3e7ee490 | 280 | |
454f18a9 | 281 | /* Check events */ |
15b2f647 | 282 | if (recv_int_page) { |
fd8b85ea | 283 | for (dword = 0; dword < maxdword; dword++) { |
15b2f647 | 284 | if (recv_int_page[dword]) { |
fd8b85ea | 285 | for (bit = 0; bit < 32; bit++) { |
15b2f647 HZ |
286 | if (test_and_clear_bit(bit, |
287 | (unsigned long *) | |
288 | &recv_int_page[dword])) { | |
3e7ee490 | 289 | relid = (dword << 5) + bit; |
3e7ee490 HJ |
290 | DPRINT_DBG(VMBUS, "event detected for relid - %d", relid); |
291 | ||
fd8b85ea GKH |
292 | if (relid == 0) { |
293 | /* special case - vmbus channel protocol msg */ | |
3e7ee490 | 294 | DPRINT_DBG(VMBUS, "invalid relid - %d", relid); |
fd8b85ea GKH |
295 | continue; |
296 | } else { | |
454f18a9 BP |
297 | /* QueueWorkItem(VmbusProcessEvent, (void*)relid); */ |
298 | /* ret = WorkQueueQueueWorkItem(gVmbusConnection.workQueue, VmbusProcessChannelEvent, (void*)relid); */ | |
c6977677 HZ |
299 | process_chn_event((void *) |
300 | (unsigned long)relid); | |
3e7ee490 HJ |
301 | } |
302 | } | |
303 | } | |
304 | } | |
305 | } | |
306 | } | |
3e7ee490 HJ |
307 | return; |
308 | } | |
309 | ||
3e189519 | 310 | /* |
c6977677 | 311 | * vmbus_post_msg - Send a msg on the vmbus's message connection |
fd8b85ea | 312 | */ |
c6977677 | 313 | int vmbus_post_msg(void *buffer, size_t buflen) |
3e7ee490 | 314 | { |
15b2f647 | 315 | union hv_connection_id conn_id; |
3e7ee490 | 316 | |
15b2f647 HZ |
317 | conn_id.asu32 = 0; |
318 | conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID; | |
319 | return hv_post_message(conn_id, 1, buffer, buflen); | |
3e7ee490 HJ |
320 | } |
321 | ||
3e189519 | 322 | /* |
c6977677 | 323 | * vmbus_set_event - Send an event notification to the parent |
fd8b85ea | 324 | */ |
c6977677 | 325 | int vmbus_set_event(u32 child_relid) |
3e7ee490 | 326 | { |
454f18a9 | 327 | /* Each u32 represents 32 channels */ |
15b2f647 HZ |
328 | set_bit(child_relid & 31, |
329 | (unsigned long *)vmbus_connection.SendInterruptPage + | |
330 | (child_relid >> 5)); | |
7c369f40 | 331 | |
d44890c8 | 332 | return hv_signal_event(); |
3e7ee490 | 333 | } |