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