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