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