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 | */ | |
23 | ||
24 | ||
a0086dc5 GKH |
25 | #include <linux/kernel.h> |
26 | #include <linux/mm.h> | |
09d50ff8 GKH |
27 | #include "include/osd.h" |
28 | #include "include/logging.h" | |
3e7ee490 HJ |
29 | |
30 | #include "VmbusPrivate.h" | |
31 | ||
454f18a9 | 32 | /* Data types */ |
3e7ee490 HJ |
33 | |
34 | typedef void (*PFN_CHANNEL_MESSAGE_HANDLER)(VMBUS_CHANNEL_MESSAGE_HEADER* msg); | |
35 | ||
36 | typedef struct _VMBUS_CHANNEL_MESSAGE_TABLE_ENTRY { | |
37 | VMBUS_CHANNEL_MESSAGE_TYPE messageType; | |
38 | PFN_CHANNEL_MESSAGE_HANDLER messageHandler; | |
39 | } VMBUS_CHANNEL_MESSAGE_TABLE_ENTRY; | |
40 | ||
454f18a9 | 41 | /* Internal routines */ |
3e7ee490 HJ |
42 | |
43 | static void | |
44 | VmbusChannelOnOffer( | |
45 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
46 | ); | |
47 | static void | |
48 | VmbusChannelOnOpenResult( | |
49 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
50 | ); | |
51 | ||
52 | static void | |
53 | VmbusChannelOnOfferRescind( | |
54 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
55 | ); | |
56 | ||
57 | static void | |
58 | VmbusChannelOnGpadlCreated( | |
59 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
60 | ); | |
61 | ||
62 | static void | |
63 | VmbusChannelOnGpadlTorndown( | |
64 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
65 | ); | |
66 | ||
67 | static void | |
68 | VmbusChannelOnOffersDelivered( | |
69 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
70 | ); | |
71 | ||
72 | static void | |
73 | VmbusChannelOnVersionResponse( | |
74 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
75 | ); | |
76 | ||
77 | static void | |
78 | VmbusChannelProcessOffer( | |
8282c400 | 79 | void * context |
3e7ee490 HJ |
80 | ); |
81 | ||
82 | static void | |
83 | VmbusChannelProcessRescindOffer( | |
8282c400 | 84 | void * context |
3e7ee490 HJ |
85 | ); |
86 | ||
87 | ||
454f18a9 | 88 | /* Globals */ |
3e7ee490 HJ |
89 | |
90 | #define MAX_NUM_DEVICE_CLASSES_SUPPORTED 4 | |
91 | ||
bd1de709 | 92 | static const GUID gSupportedDeviceClasses[MAX_NUM_DEVICE_CLASSES_SUPPORTED]= { |
454f18a9 BP |
93 | /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */ |
94 | {.Data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}},/* Storage - SCSI */ | |
95 | /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */ | |
96 | {.Data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46, 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}}, /* Network */ | |
97 | /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */ | |
98 | {.Data = {0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c, 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A}}, /* Input */ | |
99 | /* {32412632-86cb-44a2-9b5c-50d1417354f5} */ | |
100 | {.Data = {0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44, 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5}}, /* IDE */ | |
3e7ee490 HJ |
101 | |
102 | }; | |
103 | ||
454f18a9 | 104 | /* Channel message dispatch table */ |
bd1de709 | 105 | static VMBUS_CHANNEL_MESSAGE_TABLE_ENTRY gChannelMessageTable[ChannelMessageCount]= { |
3e7ee490 HJ |
106 | {ChannelMessageInvalid, NULL}, |
107 | {ChannelMessageOfferChannel, VmbusChannelOnOffer}, | |
108 | {ChannelMessageRescindChannelOffer, VmbusChannelOnOfferRescind}, | |
109 | {ChannelMessageRequestOffers, NULL}, | |
110 | {ChannelMessageAllOffersDelivered, VmbusChannelOnOffersDelivered}, | |
111 | {ChannelMessageOpenChannel, NULL}, | |
112 | {ChannelMessageOpenChannelResult, VmbusChannelOnOpenResult}, | |
113 | {ChannelMessageCloseChannel, NULL}, | |
114 | {ChannelMessageGpadlHeader, NULL}, | |
115 | {ChannelMessageGpadlBody, NULL}, | |
116 | {ChannelMessageGpadlCreated, VmbusChannelOnGpadlCreated}, | |
117 | {ChannelMessageGpadlTeardown, NULL}, | |
118 | {ChannelMessageGpadlTorndown, VmbusChannelOnGpadlTorndown}, | |
119 | {ChannelMessageRelIdReleased, NULL}, | |
120 | {ChannelMessageInitiateContact, NULL}, | |
121 | {ChannelMessageVersionResponse, VmbusChannelOnVersionResponse}, | |
122 | {ChannelMessageUnload, NULL}, | |
123 | }; | |
124 | ||
125 | /*++ | |
126 | ||
127 | Name: | |
128 | AllocVmbusChannel() | |
129 | ||
130 | Description: | |
131 | Allocate and initialize a vmbus channel object | |
132 | ||
133 | --*/ | |
aded7165 | 134 | struct vmbus_channel *AllocVmbusChannel(void) |
3e7ee490 | 135 | { |
aded7165 | 136 | struct vmbus_channel *channel; |
3e7ee490 | 137 | |
aded7165 | 138 | channel = kzalloc(sizeof(*channel), GFP_ATOMIC); |
3e7ee490 HJ |
139 | if (!channel) |
140 | { | |
141 | return NULL; | |
142 | } | |
143 | ||
54411c42 | 144 | spin_lock_init(&channel->inbound_lock); |
3e7ee490 | 145 | |
c8a429a4 GKH |
146 | init_timer(&channel->poll_timer); |
147 | channel->poll_timer.data = (unsigned long)channel; | |
148 | channel->poll_timer.function = VmbusChannelOnTimer; | |
3e7ee490 | 149 | |
454f18a9 | 150 | /* channel->dataWorkQueue = WorkQueueCreate("data"); */ |
de65a384 | 151 | channel->ControlWQ = create_workqueue("hv_vmbus_ctl"); |
3e7ee490 HJ |
152 | if (!channel->ControlWQ) |
153 | { | |
8c69f52a | 154 | kfree(channel); |
3e7ee490 HJ |
155 | return NULL; |
156 | } | |
157 | ||
158 | return channel; | |
159 | } | |
160 | ||
161 | /*++ | |
162 | ||
163 | Name: | |
164 | ReleaseVmbusChannel() | |
165 | ||
166 | Description: | |
167 | Release the vmbus channel object itself | |
168 | ||
169 | --*/ | |
170 | static inline void ReleaseVmbusChannel(void* Context) | |
171 | { | |
aded7165 | 172 | struct vmbus_channel *channel = Context; |
3e7ee490 HJ |
173 | |
174 | DPRINT_ENTER(VMBUS); | |
175 | ||
176 | DPRINT_DBG(VMBUS, "releasing channel (%p)", channel); | |
de65a384 | 177 | destroy_workqueue(channel->ControlWQ); |
3e7ee490 HJ |
178 | DPRINT_DBG(VMBUS, "channel released (%p)", channel); |
179 | ||
8c69f52a | 180 | kfree(channel); |
3e7ee490 HJ |
181 | |
182 | DPRINT_EXIT(VMBUS); | |
183 | } | |
184 | ||
185 | /*++ | |
186 | ||
187 | Name: | |
188 | FreeVmbusChannel() | |
189 | ||
190 | Description: | |
191 | Release the resources used by the vmbus channel object | |
192 | ||
193 | --*/ | |
aded7165 | 194 | void FreeVmbusChannel(struct vmbus_channel *Channel) |
3e7ee490 | 195 | { |
c8a429a4 | 196 | del_timer(&Channel->poll_timer); |
3e7ee490 | 197 | |
454f18a9 BP |
198 | /* We have to release the channel's workqueue/thread in the vmbus's workqueue/thread context */ |
199 | /* ie we can't destroy ourselves. */ | |
de65a384 BP |
200 | osd_schedule_callback(gVmbusConnection.WorkQueue, ReleaseVmbusChannel, |
201 | (void *)Channel); | |
3e7ee490 HJ |
202 | } |
203 | ||
204 | ||
205 | /*++ | |
206 | ||
207 | Name: | |
208 | VmbusChannelProcessOffer() | |
209 | ||
210 | Description: | |
211 | Process the offer by creating a channel/device associated with this offer | |
212 | ||
213 | --*/ | |
214 | static void | |
215 | VmbusChannelProcessOffer( | |
8282c400 | 216 | void * context |
3e7ee490 HJ |
217 | ) |
218 | { | |
219 | int ret=0; | |
aded7165 GKH |
220 | struct vmbus_channel *newChannel = context; |
221 | struct vmbus_channel *channel; | |
3e7ee490 HJ |
222 | LIST_ENTRY* anchor; |
223 | LIST_ENTRY* curr; | |
0e727613 | 224 | bool fNew = true; |
0f5e44ca | 225 | unsigned long flags; |
3e7ee490 HJ |
226 | |
227 | DPRINT_ENTER(VMBUS); | |
228 | ||
454f18a9 | 229 | /* Make sure this is a new offer */ |
0f5e44ca | 230 | spin_lock_irqsave(&gVmbusConnection.channel_lock, flags); |
3e7ee490 HJ |
231 | |
232 | ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelList) | |
233 | { | |
aded7165 | 234 | channel = CONTAINING_RECORD(curr, struct vmbus_channel, ListEntry); |
3e7ee490 HJ |
235 | |
236 | if (!memcmp(&channel->OfferMsg.Offer.InterfaceType, &newChannel->OfferMsg.Offer.InterfaceType,sizeof(GUID)) && | |
237 | !memcmp(&channel->OfferMsg.Offer.InterfaceInstance, &newChannel->OfferMsg.Offer.InterfaceInstance, sizeof(GUID))) | |
238 | { | |
0e727613 | 239 | fNew = false; |
3e7ee490 HJ |
240 | break; |
241 | } | |
242 | } | |
243 | ||
244 | if (fNew) | |
245 | { | |
246 | INSERT_TAIL_LIST(&gVmbusConnection.ChannelList, &newChannel->ListEntry); | |
247 | } | |
0f5e44ca | 248 | spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags); |
3e7ee490 HJ |
249 | |
250 | if (!fNew) | |
251 | { | |
252 | DPRINT_DBG(VMBUS, "Ignoring duplicate offer for relid (%d)", newChannel->OfferMsg.ChildRelId); | |
253 | FreeVmbusChannel(newChannel); | |
254 | DPRINT_EXIT(VMBUS); | |
255 | return; | |
256 | } | |
257 | ||
454f18a9 BP |
258 | /* Start the process of binding this offer to the driver */ |
259 | /* We need to set the DeviceObject field before calling VmbusChildDeviceAdd() */ | |
3e7ee490 HJ |
260 | newChannel->DeviceObject = VmbusChildDeviceCreate( |
261 | newChannel->OfferMsg.Offer.InterfaceType, | |
262 | newChannel->OfferMsg.Offer.InterfaceInstance, | |
263 | newChannel); | |
264 | ||
265 | DPRINT_DBG(VMBUS, "child device object allocated - %p", newChannel->DeviceObject); | |
266 | ||
454f18a9 BP |
267 | /* |
268 | * Add the new device to the bus. This will kick off device-driver | |
269 | * binding which eventually invokes the device driver's AddDevice() | |
270 | * method. | |
271 | */ | |
272 | ||
3e7ee490 HJ |
273 | ret = VmbusChildDeviceAdd(newChannel->DeviceObject); |
274 | if (ret != 0) | |
275 | { | |
276 | DPRINT_ERR(VMBUS, "unable to add child device object (relid %d)", | |
277 | newChannel->OfferMsg.ChildRelId); | |
278 | ||
0f5e44ca | 279 | spin_lock_irqsave(&gVmbusConnection.channel_lock, flags); |
3e7ee490 | 280 | REMOVE_ENTRY_LIST(&newChannel->ListEntry); |
0f5e44ca | 281 | spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags); |
3e7ee490 HJ |
282 | |
283 | FreeVmbusChannel(newChannel); | |
284 | } | |
285 | else | |
286 | { | |
454f18a9 BP |
287 | /* |
288 | * This state is used to indicate a successful open | |
289 | * so that when we do close the channel normally, we | |
290 | * can cleanup properly | |
291 | */ | |
3e7ee490 HJ |
292 | newChannel->State = CHANNEL_OPEN_STATE; |
293 | } | |
294 | DPRINT_EXIT(VMBUS); | |
295 | } | |
296 | ||
297 | /*++ | |
298 | ||
299 | Name: | |
300 | VmbusChannelProcessRescindOffer() | |
301 | ||
302 | Description: | |
303 | Rescind the offer by initiating a device removal | |
304 | ||
305 | --*/ | |
306 | static void | |
307 | VmbusChannelProcessRescindOffer( | |
8282c400 | 308 | void * context |
3e7ee490 HJ |
309 | ) |
310 | { | |
aded7165 | 311 | struct vmbus_channel *channel = context; |
3e7ee490 HJ |
312 | |
313 | DPRINT_ENTER(VMBUS); | |
314 | ||
315 | VmbusChildDeviceRemove(channel->DeviceObject); | |
316 | ||
317 | DPRINT_EXIT(VMBUS); | |
318 | } | |
319 | ||
320 | ||
321 | /*++ | |
322 | ||
323 | Name: | |
324 | VmbusChannelOnOffer() | |
325 | ||
326 | Description: | |
327 | Handler for channel offers from vmbus in parent partition. We ignore all offers except | |
328 | network and storage offers. For each network and storage offers, we create a channel object | |
329 | and queue a work item to the channel object to process the offer synchronously | |
330 | ||
331 | --*/ | |
332 | static void | |
333 | VmbusChannelOnOffer( | |
334 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
335 | ) | |
336 | { | |
337 | VMBUS_CHANNEL_OFFER_CHANNEL* offer = (VMBUS_CHANNEL_OFFER_CHANNEL*)hdr; | |
aded7165 | 338 | struct vmbus_channel *newChannel; |
3e7ee490 HJ |
339 | |
340 | GUID *guidType; | |
341 | GUID *guidInstance; | |
342 | int i; | |
343 | int fSupported=0; | |
344 | ||
345 | DPRINT_ENTER(VMBUS); | |
346 | ||
347 | for (i=0; i<MAX_NUM_DEVICE_CLASSES_SUPPORTED; i++) | |
348 | { | |
349 | if (memcmp(&offer->Offer.InterfaceType, &gSupportedDeviceClasses[i], sizeof(GUID)) == 0) | |
350 | { | |
351 | fSupported = 1; | |
352 | break; | |
353 | } | |
354 | } | |
355 | ||
356 | if (!fSupported) | |
357 | { | |
358 | DPRINT_DBG(VMBUS, "Ignoring channel offer notification for child relid %d", offer->ChildRelId); | |
359 | DPRINT_EXIT(VMBUS); | |
360 | ||
361 | return; | |
362 | } | |
363 | ||
364 | guidType = &offer->Offer.InterfaceType; | |
365 | guidInstance = &offer->Offer.InterfaceInstance; | |
366 | ||
367 | DPRINT_INFO(VMBUS, "Channel offer notification - child relid %d monitor id %d allocated %d, " | |
368 | "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%02x%02x} " | |
369 | "instance {%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%02x%02x}", | |
370 | offer->ChildRelId, | |
371 | offer->MonitorId, | |
372 | offer->MonitorAllocated, | |
373 | guidType->Data[3], guidType->Data[2], guidType->Data[1], guidType->Data[0], guidType->Data[5], guidType->Data[4], guidType->Data[7], guidType->Data[6], guidType->Data[8], guidType->Data[9], guidType->Data[10], guidType->Data[11], guidType->Data[12], guidType->Data[13], guidType->Data[14], guidType->Data[15], | |
374 | guidInstance->Data[3], guidInstance->Data[2], guidInstance->Data[1], guidInstance->Data[0], guidInstance->Data[5], guidInstance->Data[4], guidInstance->Data[7], guidInstance->Data[6], guidInstance->Data[8], guidInstance->Data[9], guidInstance->Data[10], guidInstance->Data[11], guidInstance->Data[12], guidInstance->Data[13], guidInstance->Data[14], guidInstance->Data[15]); | |
375 | ||
454f18a9 | 376 | /* Allocate the channel object and save this offer. */ |
3e7ee490 HJ |
377 | newChannel = AllocVmbusChannel(); |
378 | if (!newChannel) | |
379 | { | |
380 | DPRINT_ERR(VMBUS, "unable to allocate channel object"); | |
381 | return; | |
382 | } | |
383 | ||
384 | DPRINT_DBG(VMBUS, "channel object allocated - %p", newChannel); | |
385 | ||
386 | memcpy(&newChannel->OfferMsg, offer, sizeof(VMBUS_CHANNEL_OFFER_CHANNEL)); | |
5654e932 GKH |
387 | newChannel->MonitorGroup = (u8)offer->MonitorId / 32; |
388 | newChannel->MonitorBit = (u8)offer->MonitorId % 32; | |
3e7ee490 | 389 | |
454f18a9 | 390 | /* TODO: Make sure the offer comes from our parent partition */ |
de65a384 BP |
391 | osd_schedule_callback(newChannel->ControlWQ, VmbusChannelProcessOffer, |
392 | newChannel); | |
3e7ee490 HJ |
393 | |
394 | DPRINT_EXIT(VMBUS); | |
395 | } | |
396 | ||
397 | ||
398 | /*++ | |
399 | ||
400 | Name: | |
401 | VmbusChannelOnOfferRescind() | |
402 | ||
403 | Description: | |
404 | Rescind offer handler. We queue a work item to process this offer | |
405 | synchronously | |
406 | ||
407 | --*/ | |
408 | static void | |
409 | VmbusChannelOnOfferRescind( | |
410 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
411 | ) | |
412 | { | |
413 | VMBUS_CHANNEL_RESCIND_OFFER* rescind = (VMBUS_CHANNEL_RESCIND_OFFER*)hdr; | |
aded7165 | 414 | struct vmbus_channel *channel; |
3e7ee490 HJ |
415 | |
416 | DPRINT_ENTER(VMBUS); | |
417 | ||
418 | channel = GetChannelFromRelId(rescind->ChildRelId); | |
419 | if (channel == NULL) | |
420 | { | |
421 | DPRINT_DBG(VMBUS, "channel not found for relId %d", rescind->ChildRelId); | |
422 | return; | |
423 | } | |
424 | ||
de65a384 BP |
425 | osd_schedule_callback(channel->ControlWQ, |
426 | VmbusChannelProcessRescindOffer, | |
427 | channel); | |
3e7ee490 HJ |
428 | |
429 | DPRINT_EXIT(VMBUS); | |
430 | } | |
431 | ||
432 | ||
433 | /*++ | |
434 | ||
435 | Name: | |
436 | VmbusChannelOnOffersDelivered() | |
437 | ||
438 | Description: | |
439 | This is invoked when all offers have been delivered. | |
440 | Nothing to do here. | |
441 | ||
442 | --*/ | |
443 | static void | |
444 | VmbusChannelOnOffersDelivered( | |
445 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
446 | ) | |
447 | { | |
448 | DPRINT_ENTER(VMBUS); | |
449 | DPRINT_EXIT(VMBUS); | |
450 | } | |
451 | ||
452 | ||
453 | /*++ | |
454 | ||
455 | Name: | |
456 | VmbusChannelOnOpenResult() | |
457 | ||
458 | Description: | |
459 | Open result handler. This is invoked when we received a response | |
460 | to our channel open request. Find the matching request, copy the | |
461 | response and signal the requesting thread. | |
462 | ||
463 | --*/ | |
464 | static void | |
465 | VmbusChannelOnOpenResult( | |
466 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
467 | ) | |
468 | { | |
469 | VMBUS_CHANNEL_OPEN_RESULT* result = (VMBUS_CHANNEL_OPEN_RESULT*)hdr; | |
470 | LIST_ENTRY* anchor; | |
471 | LIST_ENTRY* curr; | |
aded7165 | 472 | struct vmbus_channel_msginfo *msgInfo; |
3e7ee490 HJ |
473 | VMBUS_CHANNEL_MESSAGE_HEADER* requestHeader; |
474 | VMBUS_CHANNEL_OPEN_CHANNEL* openMsg; | |
dd0813b6 | 475 | unsigned long flags; |
3e7ee490 HJ |
476 | |
477 | DPRINT_ENTER(VMBUS); | |
478 | ||
479 | DPRINT_DBG(VMBUS, "vmbus open result - %d", result->Status); | |
480 | ||
454f18a9 | 481 | /* Find the open msg, copy the result and signal/unblock the wait event */ |
dd0813b6 | 482 | spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags); |
3e7ee490 HJ |
483 | |
484 | ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList) | |
485 | { | |
aded7165 | 486 | msgInfo = (struct vmbus_channel_msginfo *)curr; |
3e7ee490 HJ |
487 | requestHeader = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg; |
488 | ||
489 | if (requestHeader->MessageType == ChannelMessageOpenChannel) | |
490 | { | |
491 | openMsg = (VMBUS_CHANNEL_OPEN_CHANNEL*)msgInfo->Msg; | |
492 | if (openMsg->ChildRelId == result->ChildRelId && | |
493 | openMsg->OpenId == result->OpenId) | |
494 | { | |
495 | memcpy(&msgInfo->Response.OpenResult, result, sizeof(VMBUS_CHANNEL_OPEN_RESULT)); | |
bfc30aae | 496 | osd_WaitEventSet(msgInfo->WaitEvent); |
3e7ee490 HJ |
497 | break; |
498 | } | |
499 | } | |
500 | } | |
dd0813b6 | 501 | spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags); |
3e7ee490 HJ |
502 | |
503 | DPRINT_EXIT(VMBUS); | |
504 | } | |
505 | ||
506 | ||
507 | /*++ | |
508 | ||
509 | Name: | |
510 | VmbusChannelOnGpadlCreated() | |
511 | ||
512 | Description: | |
513 | GPADL created handler. This is invoked when we received a response | |
514 | to our gpadl create request. Find the matching request, copy the | |
515 | response and signal the requesting thread. | |
516 | ||
517 | --*/ | |
518 | static void | |
519 | VmbusChannelOnGpadlCreated( | |
520 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
521 | ) | |
522 | { | |
523 | VMBUS_CHANNEL_GPADL_CREATED *gpadlCreated = (VMBUS_CHANNEL_GPADL_CREATED*)hdr; | |
524 | LIST_ENTRY *anchor; | |
525 | LIST_ENTRY *curr; | |
aded7165 | 526 | struct vmbus_channel_msginfo *msgInfo; |
3e7ee490 HJ |
527 | VMBUS_CHANNEL_MESSAGE_HEADER *requestHeader; |
528 | VMBUS_CHANNEL_GPADL_HEADER *gpadlHeader; | |
dd0813b6 | 529 | unsigned long flags; |
3e7ee490 HJ |
530 | |
531 | DPRINT_ENTER(VMBUS); | |
532 | ||
533 | DPRINT_DBG(VMBUS, "vmbus gpadl created result - %d", gpadlCreated->CreationStatus); | |
534 | ||
454f18a9 | 535 | /* Find the establish msg, copy the result and signal/unblock the wait event */ |
dd0813b6 | 536 | spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags); |
3e7ee490 HJ |
537 | |
538 | ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList) | |
539 | { | |
aded7165 | 540 | msgInfo = (struct vmbus_channel_msginfo *)curr; |
3e7ee490 HJ |
541 | requestHeader = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg; |
542 | ||
543 | if (requestHeader->MessageType == ChannelMessageGpadlHeader) | |
544 | { | |
545 | gpadlHeader = (VMBUS_CHANNEL_GPADL_HEADER*)requestHeader; | |
546 | ||
547 | if ((gpadlCreated->ChildRelId == gpadlHeader->ChildRelId) && | |
548 | (gpadlCreated->Gpadl == gpadlHeader->Gpadl)) | |
549 | { | |
550 | memcpy(&msgInfo->Response.GpadlCreated, gpadlCreated, sizeof(VMBUS_CHANNEL_GPADL_CREATED)); | |
bfc30aae | 551 | osd_WaitEventSet(msgInfo->WaitEvent); |
3e7ee490 HJ |
552 | break; |
553 | } | |
554 | } | |
555 | } | |
dd0813b6 | 556 | spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags); |
3e7ee490 HJ |
557 | |
558 | DPRINT_EXIT(VMBUS); | |
559 | } | |
560 | ||
561 | ||
562 | /*++ | |
563 | ||
564 | Name: | |
565 | VmbusChannelOnGpadlTorndown() | |
566 | ||
567 | Description: | |
568 | GPADL torndown handler. This is invoked when we received a response | |
569 | to our gpadl teardown request. Find the matching request, copy the | |
570 | response and signal the requesting thread. | |
571 | ||
572 | --*/ | |
573 | static void | |
574 | VmbusChannelOnGpadlTorndown( | |
575 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
576 | ) | |
577 | { | |
578 | VMBUS_CHANNEL_GPADL_TORNDOWN* gpadlTorndown = (VMBUS_CHANNEL_GPADL_TORNDOWN*)hdr; | |
579 | LIST_ENTRY* anchor; | |
580 | LIST_ENTRY* curr; | |
aded7165 | 581 | struct vmbus_channel_msginfo *msgInfo; |
3e7ee490 HJ |
582 | VMBUS_CHANNEL_MESSAGE_HEADER *requestHeader; |
583 | VMBUS_CHANNEL_GPADL_TEARDOWN *gpadlTeardown; | |
dd0813b6 | 584 | unsigned long flags; |
3e7ee490 HJ |
585 | |
586 | DPRINT_ENTER(VMBUS); | |
587 | ||
454f18a9 | 588 | /* Find the open msg, copy the result and signal/unblock the wait event */ |
dd0813b6 | 589 | spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags); |
3e7ee490 HJ |
590 | |
591 | ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList) | |
592 | { | |
aded7165 | 593 | msgInfo = (struct vmbus_channel_msginfo *)curr; |
3e7ee490 HJ |
594 | requestHeader = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg; |
595 | ||
596 | if (requestHeader->MessageType == ChannelMessageGpadlTeardown) | |
597 | { | |
598 | gpadlTeardown = (VMBUS_CHANNEL_GPADL_TEARDOWN*)requestHeader; | |
599 | ||
600 | if (gpadlTorndown->Gpadl == gpadlTeardown->Gpadl) | |
601 | { | |
602 | memcpy(&msgInfo->Response.GpadlTorndown, gpadlTorndown, sizeof(VMBUS_CHANNEL_GPADL_TORNDOWN)); | |
bfc30aae | 603 | osd_WaitEventSet(msgInfo->WaitEvent); |
3e7ee490 HJ |
604 | break; |
605 | } | |
606 | } | |
607 | } | |
dd0813b6 | 608 | spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags); |
3e7ee490 HJ |
609 | |
610 | DPRINT_EXIT(VMBUS); | |
611 | } | |
612 | ||
613 | ||
614 | /*++ | |
615 | ||
616 | Name: | |
617 | VmbusChannelOnVersionResponse() | |
618 | ||
619 | Description: | |
620 | Version response handler. This is invoked when we received a response | |
621 | to our initiate contact request. Find the matching request, copy the | |
622 | response and signal the requesting thread. | |
623 | ||
624 | --*/ | |
625 | static void | |
626 | VmbusChannelOnVersionResponse( | |
627 | PVMBUS_CHANNEL_MESSAGE_HEADER hdr | |
628 | ) | |
629 | { | |
630 | LIST_ENTRY* anchor; | |
631 | LIST_ENTRY* curr; | |
aded7165 | 632 | struct vmbus_channel_msginfo *msgInfo; |
3e7ee490 HJ |
633 | VMBUS_CHANNEL_MESSAGE_HEADER *requestHeader; |
634 | VMBUS_CHANNEL_INITIATE_CONTACT *initiate; | |
635 | VMBUS_CHANNEL_VERSION_RESPONSE *versionResponse = (VMBUS_CHANNEL_VERSION_RESPONSE*)hdr; | |
dd0813b6 | 636 | unsigned long flags; |
3e7ee490 HJ |
637 | |
638 | DPRINT_ENTER(VMBUS); | |
639 | ||
dd0813b6 | 640 | spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags); |
3e7ee490 HJ |
641 | |
642 | ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList) | |
643 | { | |
aded7165 | 644 | msgInfo = (struct vmbus_channel_msginfo *)curr; |
3e7ee490 HJ |
645 | requestHeader = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg; |
646 | ||
647 | if (requestHeader->MessageType == ChannelMessageInitiateContact) | |
648 | { | |
649 | initiate = (VMBUS_CHANNEL_INITIATE_CONTACT*)requestHeader; | |
650 | memcpy(&msgInfo->Response.VersionResponse, versionResponse, sizeof(VMBUS_CHANNEL_VERSION_RESPONSE)); | |
bfc30aae | 651 | osd_WaitEventSet(msgInfo->WaitEvent); |
3e7ee490 HJ |
652 | } |
653 | } | |
dd0813b6 | 654 | spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags); |
3e7ee490 HJ |
655 | |
656 | DPRINT_EXIT(VMBUS); | |
657 | } | |
658 | ||
659 | ||
660 | /*++ | |
661 | ||
662 | Name: | |
663 | VmbusOnChannelMessage() | |
664 | ||
665 | Description: | |
666 | Handler for channel protocol messages. | |
667 | This is invoked in the vmbus worker thread context. | |
668 | ||
669 | --*/ | |
b239549c | 670 | void VmbusOnChannelMessage(void *Context) |
3e7ee490 HJ |
671 | { |
672 | HV_MESSAGE *msg=(HV_MESSAGE*)Context; | |
673 | VMBUS_CHANNEL_MESSAGE_HEADER* hdr; | |
674 | int size; | |
675 | ||
676 | DPRINT_ENTER(VMBUS); | |
677 | ||
678 | hdr = (VMBUS_CHANNEL_MESSAGE_HEADER*)msg->u.Payload; | |
679 | size=msg->Header.PayloadSize; | |
680 | ||
681 | DPRINT_DBG(VMBUS, "message type %d size %d", hdr->MessageType, size); | |
682 | ||
683 | if (hdr->MessageType >= ChannelMessageCount) | |
684 | { | |
685 | DPRINT_ERR(VMBUS, "Received invalid channel message type %d size %d", hdr->MessageType, size); | |
04f50c4d GKH |
686 | print_hex_dump_bytes("", DUMP_PREFIX_NONE, |
687 | (unsigned char *)msg->u.Payload, size); | |
8c69f52a | 688 | kfree(msg); |
3e7ee490 HJ |
689 | return; |
690 | } | |
691 | ||
692 | if (gChannelMessageTable[hdr->MessageType].messageHandler) | |
693 | { | |
694 | gChannelMessageTable[hdr->MessageType].messageHandler(hdr); | |
695 | } | |
696 | else | |
697 | { | |
698 | DPRINT_ERR(VMBUS, "Unhandled channel message type %d", hdr->MessageType); | |
699 | } | |
700 | ||
454f18a9 | 701 | /* Free the msg that was allocated in VmbusOnMsgDPC() */ |
8c69f52a | 702 | kfree(msg); |
3e7ee490 HJ |
703 | DPRINT_EXIT(VMBUS); |
704 | } | |
705 | ||
706 | ||
707 | /*++ | |
708 | ||
709 | Name: | |
710 | VmbusChannelRequestOffers() | |
711 | ||
712 | Description: | |
713 | Send a request to get all our pending offers. | |
714 | ||
715 | --*/ | |
b239549c | 716 | int VmbusChannelRequestOffers(void) |
3e7ee490 HJ |
717 | { |
718 | int ret=0; | |
719 | VMBUS_CHANNEL_MESSAGE_HEADER* msg; | |
aded7165 | 720 | struct vmbus_channel_msginfo *msgInfo; |
3e7ee490 HJ |
721 | |
722 | DPRINT_ENTER(VMBUS); | |
723 | ||
aded7165 | 724 | msgInfo = kmalloc(sizeof(*msgInfo) + sizeof(VMBUS_CHANNEL_MESSAGE_HEADER), GFP_KERNEL); |
3e7ee490 HJ |
725 | ASSERT(msgInfo != NULL); |
726 | ||
bfc30aae | 727 | msgInfo->WaitEvent = osd_WaitEventCreate(); |
3e7ee490 HJ |
728 | msg = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg; |
729 | ||
730 | msg->MessageType = ChannelMessageRequestOffers; | |
731 | ||
732 | /*SpinlockAcquire(gVmbusConnection.channelMsgLock); | |
733 | INSERT_TAIL_LIST(&gVmbusConnection.channelMsgList, &msgInfo->msgListEntry); | |
734 | SpinlockRelease(gVmbusConnection.channelMsgLock);*/ | |
735 | ||
736 | ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_MESSAGE_HEADER)); | |
737 | if (ret != 0) | |
738 | { | |
739 | DPRINT_ERR(VMBUS, "Unable to request offers - %d", ret); | |
740 | ||
741 | /*SpinlockAcquire(gVmbusConnection.channelMsgLock); | |
742 | REMOVE_ENTRY_LIST(&msgInfo->msgListEntry); | |
743 | SpinlockRelease(gVmbusConnection.channelMsgLock);*/ | |
744 | ||
745 | goto Cleanup; | |
746 | } | |
bfc30aae | 747 | /* osd_WaitEventWait(msgInfo->waitEvent); */ |
3e7ee490 HJ |
748 | |
749 | /*SpinlockAcquire(gVmbusConnection.channelMsgLock); | |
750 | REMOVE_ENTRY_LIST(&msgInfo->msgListEntry); | |
751 | SpinlockRelease(gVmbusConnection.channelMsgLock);*/ | |
752 | ||
753 | ||
754 | Cleanup: | |
755 | if (msgInfo) | |
756 | { | |
420beac4 | 757 | kfree(msgInfo->WaitEvent); |
8c69f52a | 758 | kfree(msgInfo); |
3e7ee490 HJ |
759 | } |
760 | ||
761 | DPRINT_EXIT(VMBUS); | |
762 | ||
763 | return ret; | |
764 | } | |
765 | ||
766 | /*++ | |
767 | ||
768 | Name: | |
769 | VmbusChannelReleaseUnattachedChannels() | |
770 | ||
771 | Description: | |
772 | Release channels that are unattached/unconnected ie (no drivers associated) | |
773 | ||
774 | --*/ | |
b239549c | 775 | void VmbusChannelReleaseUnattachedChannels(void) |
3e7ee490 HJ |
776 | { |
777 | LIST_ENTRY *entry; | |
aded7165 GKH |
778 | struct vmbus_channel *channel; |
779 | struct vmbus_channel *start = NULL; | |
0f5e44ca | 780 | unsigned long flags; |
3e7ee490 | 781 | |
0f5e44ca | 782 | spin_lock_irqsave(&gVmbusConnection.channel_lock, flags); |
3e7ee490 HJ |
783 | |
784 | while (!IsListEmpty(&gVmbusConnection.ChannelList)) | |
785 | { | |
786 | entry = TOP_LIST_ENTRY(&gVmbusConnection.ChannelList); | |
aded7165 | 787 | channel = CONTAINING_RECORD(entry, struct vmbus_channel, ListEntry); |
3e7ee490 HJ |
788 | |
789 | if (channel == start) | |
790 | break; | |
791 | ||
792 | if (!channel->DeviceObject->Driver) | |
793 | { | |
794 | REMOVE_ENTRY_LIST(&channel->ListEntry); | |
795 | DPRINT_INFO(VMBUS, "Releasing unattached device object %p", channel->DeviceObject); | |
796 | ||
797 | VmbusChildDeviceRemove(channel->DeviceObject); | |
798 | FreeVmbusChannel(channel); | |
799 | } | |
800 | else | |
801 | { | |
802 | if (!start) | |
803 | { | |
804 | start = channel; | |
805 | } | |
806 | } | |
807 | } | |
808 | ||
0f5e44ca | 809 | spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags); |
3e7ee490 HJ |
810 | } |
811 | ||
454f18a9 | 812 | /* eof */ |