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" |
3e7ee490 HJ |
29 | #include "VmbusPrivate.h" |
30 | ||
3e7ee490 | 31 | |
662e66b0 | 32 | struct VMBUS_CONNECTION gVmbusConnection = { |
3e7ee490 | 33 | .ConnectState = Disconnected, |
f4888417 | 34 | .NextGpadlHandle = ATOMIC_INIT(0xE1E10), |
3e7ee490 HJ |
35 | }; |
36 | ||
fd8b85ea GKH |
37 | /** |
38 | * VmbusConnect - Sends a connect request on the partition service connection | |
39 | */ | |
f346fdc2 | 40 | int VmbusConnect(void) |
3e7ee490 | 41 | { |
fd8b85ea | 42 | int ret = 0; |
aded7165 | 43 | struct vmbus_channel_msginfo *msgInfo = NULL; |
82250213 | 44 | struct vmbus_channel_initiate_contact *msg; |
dd0813b6 | 45 | unsigned long flags; |
3e7ee490 HJ |
46 | |
47 | DPRINT_ENTER(VMBUS); | |
48 | ||
454f18a9 | 49 | /* Make sure we are not connecting or connected */ |
3e7ee490 HJ |
50 | if (gVmbusConnection.ConnectState != Disconnected) |
51 | return -1; | |
52 | ||
454f18a9 | 53 | /* Initialize the vmbus connection */ |
3e7ee490 | 54 | gVmbusConnection.ConnectState = Connecting; |
de65a384 | 55 | gVmbusConnection.WorkQueue = create_workqueue("hv_vmbus_con"); |
fd8b85ea | 56 | if (!gVmbusConnection.WorkQueue) { |
de65a384 BP |
57 | ret = -1; |
58 | goto Cleanup; | |
59 | } | |
3e7ee490 | 60 | |
53af545b | 61 | INIT_LIST_HEAD(&gVmbusConnection.ChannelMsgList); |
dd0813b6 | 62 | spin_lock_init(&gVmbusConnection.channelmsg_lock); |
3e7ee490 | 63 | |
53af545b | 64 | INIT_LIST_HEAD(&gVmbusConnection.ChannelList); |
0f5e44ca | 65 | spin_lock_init(&gVmbusConnection.channel_lock); |
3e7ee490 | 66 | |
454f18a9 BP |
67 | /* |
68 | * Setup the vmbus event connection for channel interrupt | |
69 | * abstraction stuff | |
70 | */ | |
bfc30aae | 71 | gVmbusConnection.InterruptPage = osd_PageAlloc(1); |
fd8b85ea | 72 | if (gVmbusConnection.InterruptPage == NULL) { |
3e7ee490 HJ |
73 | ret = -1; |
74 | goto Cleanup; | |
75 | } | |
76 | ||
77 | gVmbusConnection.RecvInterruptPage = gVmbusConnection.InterruptPage; | |
fd8b85ea GKH |
78 | gVmbusConnection.SendInterruptPage = |
79 | (void *)((unsigned long)gVmbusConnection.InterruptPage + | |
80 | (PAGE_SIZE >> 1)); | |
3e7ee490 | 81 | |
fd8b85ea GKH |
82 | /* |
83 | * Setup the monitor notification facility. The 1st page for | |
84 | * parent->child and the 2nd page for child->parent | |
454f18a9 | 85 | */ |
bfc30aae | 86 | gVmbusConnection.MonitorPages = osd_PageAlloc(2); |
fd8b85ea | 87 | if (gVmbusConnection.MonitorPages == NULL) { |
3e7ee490 HJ |
88 | ret = -1; |
89 | goto Cleanup; | |
90 | } | |
91 | ||
fd8b85ea GKH |
92 | msgInfo = kzalloc(sizeof(*msgInfo) + |
93 | sizeof(struct vmbus_channel_initiate_contact), | |
94 | GFP_KERNEL); | |
95 | if (msgInfo == NULL) { | |
3e7ee490 HJ |
96 | ret = -1; |
97 | goto Cleanup; | |
98 | } | |
99 | ||
bfc30aae | 100 | msgInfo->WaitEvent = osd_WaitEventCreate(); |
82250213 | 101 | msg = (struct vmbus_channel_initiate_contact *)msgInfo->Msg; |
3e7ee490 HJ |
102 | |
103 | msg->Header.MessageType = ChannelMessageInitiateContact; | |
104 | msg->VMBusVersionRequested = VMBUS_REVISION_NUMBER; | |
fa56d361 GKH |
105 | msg->InterruptPage = virt_to_phys(gVmbusConnection.InterruptPage); |
106 | msg->MonitorPage1 = virt_to_phys(gVmbusConnection.MonitorPages); | |
fd8b85ea GKH |
107 | msg->MonitorPage2 = virt_to_phys( |
108 | (void *)((unsigned long)gVmbusConnection.MonitorPages + | |
109 | PAGE_SIZE)); | |
3e7ee490 | 110 | |
454f18a9 BP |
111 | /* |
112 | * Add to list before we send the request since we may | |
113 | * receive the response before returning from this routine | |
114 | */ | |
dd0813b6 | 115 | spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags); |
53af545b BP |
116 | list_add_tail(&msgInfo->MsgListEntry, |
117 | &gVmbusConnection.ChannelMsgList); | |
118 | ||
dd0813b6 | 119 | spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags); |
3e7ee490 | 120 | |
fd8b85ea GKH |
121 | DPRINT_DBG(VMBUS, "Vmbus connection - interrupt pfn %llx, " |
122 | "monitor1 pfn %llx,, monitor2 pfn %llx", | |
123 | msg->InterruptPage, msg->MonitorPage1, msg->MonitorPage2); | |
3e7ee490 HJ |
124 | |
125 | DPRINT_DBG(VMBUS, "Sending channel initiate msg..."); | |
fd8b85ea GKH |
126 | ret = VmbusPostMessage(msg, |
127 | sizeof(struct vmbus_channel_initiate_contact)); | |
128 | if (ret != 0) { | |
53af545b | 129 | list_del(&msgInfo->MsgListEntry); |
3e7ee490 HJ |
130 | goto Cleanup; |
131 | } | |
132 | ||
454f18a9 | 133 | /* Wait for the connection response */ |
bfc30aae | 134 | osd_WaitEventWait(msgInfo->WaitEvent); |
3e7ee490 | 135 | |
53af545b | 136 | list_del(&msgInfo->MsgListEntry); |
3e7ee490 | 137 | |
454f18a9 | 138 | /* Check if successful */ |
fd8b85ea | 139 | if (msgInfo->Response.VersionResponse.VersionSupported) { |
3e7ee490 HJ |
140 | DPRINT_INFO(VMBUS, "Vmbus connected!!"); |
141 | gVmbusConnection.ConnectState = Connected; | |
142 | ||
fd8b85ea GKH |
143 | } else { |
144 | DPRINT_ERR(VMBUS, "Vmbus connection failed!!..." | |
145 | "current version (%d) not supported", | |
146 | VMBUS_REVISION_NUMBER); | |
3e7ee490 | 147 | ret = -1; |
3e7ee490 HJ |
148 | goto Cleanup; |
149 | } | |
150 | ||
420beac4 | 151 | kfree(msgInfo->WaitEvent); |
8c69f52a | 152 | kfree(msgInfo); |
3e7ee490 HJ |
153 | DPRINT_EXIT(VMBUS); |
154 | ||
155 | return 0; | |
156 | ||
157 | Cleanup: | |
3e7ee490 HJ |
158 | gVmbusConnection.ConnectState = Disconnected; |
159 | ||
de65a384 BP |
160 | if (gVmbusConnection.WorkQueue) |
161 | destroy_workqueue(gVmbusConnection.WorkQueue); | |
3e7ee490 | 162 | |
fd8b85ea | 163 | if (gVmbusConnection.InterruptPage) { |
bfc30aae | 164 | osd_PageFree(gVmbusConnection.InterruptPage, 1); |
3e7ee490 HJ |
165 | gVmbusConnection.InterruptPage = NULL; |
166 | } | |
167 | ||
fd8b85ea | 168 | if (gVmbusConnection.MonitorPages) { |
bfc30aae | 169 | osd_PageFree(gVmbusConnection.MonitorPages, 2); |
3e7ee490 HJ |
170 | gVmbusConnection.MonitorPages = NULL; |
171 | } | |
172 | ||
fd8b85ea GKH |
173 | if (msgInfo) { |
174 | kfree(msgInfo->WaitEvent); | |
8c69f52a | 175 | kfree(msgInfo); |
3e7ee490 HJ |
176 | } |
177 | ||
178 | DPRINT_EXIT(VMBUS); | |
179 | ||
180 | return ret; | |
181 | } | |
182 | ||
fd8b85ea GKH |
183 | /** |
184 | * VmbusDisconnect - Sends a disconnect request on the partition service connection | |
185 | */ | |
f346fdc2 | 186 | int VmbusDisconnect(void) |
3e7ee490 | 187 | { |
fd8b85ea | 188 | int ret = 0; |
82250213 | 189 | struct vmbus_channel_message_header *msg; |
3e7ee490 HJ |
190 | |
191 | DPRINT_ENTER(VMBUS); | |
192 | ||
454f18a9 | 193 | /* Make sure we are connected */ |
3e7ee490 HJ |
194 | if (gVmbusConnection.ConnectState != Connected) |
195 | return -1; | |
196 | ||
82250213 | 197 | msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL); |
3e7ee490 HJ |
198 | |
199 | msg->MessageType = ChannelMessageUnload; | |
200 | ||
fd8b85ea GKH |
201 | ret = VmbusPostMessage(msg, |
202 | sizeof(struct vmbus_channel_message_header)); | |
3e7ee490 | 203 | if (ret != 0) |
3e7ee490 | 204 | goto Cleanup; |
3e7ee490 | 205 | |
bfc30aae | 206 | osd_PageFree(gVmbusConnection.InterruptPage, 1); |
3e7ee490 | 207 | |
454f18a9 | 208 | /* TODO: iterate thru the msg list and free up */ |
de65a384 | 209 | destroy_workqueue(gVmbusConnection.WorkQueue); |
3e7ee490 HJ |
210 | |
211 | gVmbusConnection.ConnectState = Disconnected; | |
212 | ||
213 | DPRINT_INFO(VMBUS, "Vmbus disconnected!!"); | |
214 | ||
215 | Cleanup: | |
fd8b85ea | 216 | kfree(msg); |
3e7ee490 | 217 | DPRINT_EXIT(VMBUS); |
3e7ee490 HJ |
218 | return ret; |
219 | } | |
220 | ||
fd8b85ea GKH |
221 | /** |
222 | * GetChannelFromRelId - Get the channel object given its child relative id (ie channel id) | |
223 | */ | |
aded7165 | 224 | struct vmbus_channel *GetChannelFromRelId(u32 relId) |
3e7ee490 | 225 | { |
aded7165 GKH |
226 | struct vmbus_channel *channel; |
227 | struct vmbus_channel *foundChannel = NULL; | |
0f5e44ca | 228 | unsigned long flags; |
3e7ee490 | 229 | |
0f5e44ca | 230 | spin_lock_irqsave(&gVmbusConnection.channel_lock, flags); |
53af545b | 231 | list_for_each_entry(channel, &gVmbusConnection.ChannelList, ListEntry) { |
fd8b85ea | 232 | if (channel->OfferMsg.ChildRelId == relId) { |
3e7ee490 HJ |
233 | foundChannel = channel; |
234 | break; | |
235 | } | |
236 | } | |
0f5e44ca | 237 | spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags); |
3e7ee490 HJ |
238 | |
239 | return foundChannel; | |
240 | } | |
241 | ||
fd8b85ea GKH |
242 | /** |
243 | * VmbusProcessChannelEvent - Process a channel event notification | |
244 | */ | |
245 | static void VmbusProcessChannelEvent(void *context) | |
3e7ee490 | 246 | { |
aded7165 | 247 | struct vmbus_channel *channel; |
c4b0bc94 | 248 | u32 relId = (u32)(unsigned long)context; |
3e7ee490 HJ |
249 | |
250 | ASSERT(relId > 0); | |
251 | ||
454f18a9 BP |
252 | /* |
253 | * Find the channel based on this relid and invokes the | |
254 | * channel callback to process the event | |
255 | */ | |
3e7ee490 HJ |
256 | channel = GetChannelFromRelId(relId); |
257 | ||
fd8b85ea | 258 | if (channel) { |
3e7ee490 | 259 | VmbusChannelOnChannelEvent(channel); |
fd8b85ea GKH |
260 | /* |
261 | * WorkQueueQueueWorkItem(channel->dataWorkQueue, | |
262 | * VmbusChannelOnChannelEvent, | |
263 | * (void*)channel); | |
264 | */ | |
265 | } else { | |
266 | DPRINT_ERR(VMBUS, "channel not found for relid - %d.", relId); | |
3e7ee490 HJ |
267 | } |
268 | } | |
269 | ||
fd8b85ea GKH |
270 | /** |
271 | * VmbusOnEvents - Handler for events | |
272 | */ | |
f346fdc2 | 273 | void VmbusOnEvents(void) |
3e7ee490 HJ |
274 | { |
275 | int dword; | |
3e7ee490 HJ |
276 | int maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5; |
277 | int bit; | |
278 | int relid; | |
fd8b85ea | 279 | u32 *recvInterruptPage = gVmbusConnection.RecvInterruptPage; |
3e7ee490 HJ |
280 | |
281 | DPRINT_ENTER(VMBUS); | |
282 | ||
454f18a9 | 283 | /* Check events */ |
fd8b85ea GKH |
284 | if (recvInterruptPage) { |
285 | for (dword = 0; dword < maxdword; dword++) { | |
286 | if (recvInterruptPage[dword]) { | |
287 | for (bit = 0; bit < 32; bit++) { | |
288 | if (test_and_clear_bit(bit, (unsigned long *)&recvInterruptPage[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); */ | |
fd8b85ea | 299 | VmbusProcessChannelEvent((void *)(unsigned long)relid); |
3e7ee490 HJ |
300 | } |
301 | } | |
302 | } | |
303 | } | |
304 | } | |
305 | } | |
306 | DPRINT_EXIT(VMBUS); | |
307 | ||
308 | return; | |
309 | } | |
310 | ||
fd8b85ea GKH |
311 | /** |
312 | * VmbusPostMessage - Send a msg on the vmbus's message connection | |
313 | */ | |
f346fdc2 | 314 | int VmbusPostMessage(void *buffer, size_t bufferLen) |
3e7ee490 | 315 | { |
eacb1b4d | 316 | union hv_connection_id connId; |
3e7ee490 | 317 | |
fd8b85ea | 318 | connId.Asu32 = 0; |
3e7ee490 | 319 | connId.u.Id = VMBUS_MESSAGE_CONNECTION_ID; |
fd8b85ea | 320 | return HvPostMessage(connId, 1, buffer, bufferLen); |
3e7ee490 HJ |
321 | } |
322 | ||
fd8b85ea GKH |
323 | /** |
324 | * VmbusSetEvent - Send an event notification to the parent | |
325 | */ | |
f346fdc2 | 326 | int VmbusSetEvent(u32 childRelId) |
3e7ee490 | 327 | { |
fd8b85ea | 328 | int ret = 0; |
3e7ee490 HJ |
329 | |
330 | DPRINT_ENTER(VMBUS); | |
331 | ||
454f18a9 | 332 | /* Each u32 represents 32 channels */ |
7c369f40 | 333 | set_bit(childRelId & 31, |
fd8b85ea GKH |
334 | (unsigned long *)gVmbusConnection.SendInterruptPage + |
335 | (childRelId >> 5)); | |
7c369f40 | 336 | |
3e7ee490 HJ |
337 | ret = HvSignalEvent(); |
338 | ||
339 | DPRINT_EXIT(VMBUS); | |
340 | ||
341 | return ret; | |
342 | } |