]> Pileus Git - ~andy/linux/blob - drivers/staging/hv/ChannelMgmt.c
Staging: hv: make gVmbusConnection.ChannelMsgLock a real spinlock
[~andy/linux] / drivers / staging / hv / ChannelMgmt.c
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 "include/osd.h"
26 #include "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         void * context
86         );
87
88 static void
89 VmbusChannelProcessRescindOffer(
90         void * 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 = kzalloc(sizeof(VMBUS_CHANNEL), GFP_ATOMIC);
147         if (!channel)
148         {
149                 return NULL;
150         }
151
152         spin_lock_init(&channel->inbound_lock);
153
154         channel->PollTimer = TimerCreate(VmbusChannelOnTimer, channel);
155         if (!channel->PollTimer)
156         {
157                 kfree(channel);
158                 return NULL;
159         }
160
161         //channel->dataWorkQueue = WorkQueueCreate("data");
162         channel->ControlWQ = WorkQueueCreate("control");
163         if (!channel->ControlWQ)
164         {
165                 TimerClose(channel->PollTimer);
166                 kfree(channel);
167                 return NULL;
168         }
169
170         return channel;
171 }
172
173 /*++
174
175 Name:
176         ReleaseVmbusChannel()
177
178 Description:
179         Release the vmbus channel object itself
180
181 --*/
182 static 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
192         kfree(channel);
193
194         DPRINT_EXIT(VMBUS);
195 }
196
197 /*++
198
199 Name:
200         FreeVmbusChannel()
201
202 Description:
203         Release the resources used by the vmbus channel object
204
205 --*/
206 void FreeVmbusChannel(VMBUS_CHANNEL* Channel)
207 {
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
218 Name:
219         VmbusChannelProcessOffer()
220
221 Description:
222         Process the offer by creating a channel/device associated with this offer
223
224 --*/
225 static void
226 VmbusChannelProcessOffer(
227         void * context
228         )
229 {
230         int ret=0;
231         VMBUS_CHANNEL* newChannel=(VMBUS_CHANNEL*)context;
232         LIST_ENTRY* anchor;
233         LIST_ENTRY* curr;
234         bool fNew = true;
235         VMBUS_CHANNEL* channel;
236
237         DPRINT_ENTER(VMBUS);
238
239         // Make sure this is a new offer
240         SpinlockAcquire(gVmbusConnection.ChannelLock);
241
242         ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelList)
243         {
244                 channel = CONTAINING_RECORD(curr, VMBUS_CHANNEL, ListEntry);
245
246                 if (!memcmp(&channel->OfferMsg.Offer.InterfaceType, &newChannel->OfferMsg.Offer.InterfaceType,sizeof(GUID)) &&
247                         !memcmp(&channel->OfferMsg.Offer.InterfaceInstance, &newChannel->OfferMsg.Offer.InterfaceInstance, sizeof(GUID)))
248                 {
249                         fNew = false;
250                         break;
251                 }
252         }
253
254         if (fNew)
255         {
256                 INSERT_TAIL_LIST(&gVmbusConnection.ChannelList, &newChannel->ListEntry);
257         }
258         SpinlockRelease(gVmbusConnection.ChannelLock);
259
260         if (!fNew)
261         {
262                 DPRINT_DBG(VMBUS, "Ignoring duplicate offer for relid (%d)", newChannel->OfferMsg.ChildRelId);
263                 FreeVmbusChannel(newChannel);
264                 DPRINT_EXIT(VMBUS);
265                 return;
266         }
267
268         // Start the process of binding this offer to the driver
269         // We need to set the DeviceObject field before calling VmbusChildDeviceAdd()
270         newChannel->DeviceObject = VmbusChildDeviceCreate(
271                 newChannel->OfferMsg.Offer.InterfaceType,
272                 newChannel->OfferMsg.Offer.InterfaceInstance,
273                 newChannel);
274
275         DPRINT_DBG(VMBUS, "child device object allocated - %p", newChannel->DeviceObject);
276
277         // Add the new device to the bus. This will kick off device-driver binding
278         // which eventually invokes the device driver's AddDevice() method.
279         ret = VmbusChildDeviceAdd(newChannel->DeviceObject);
280         if (ret != 0)
281         {
282                 DPRINT_ERR(VMBUS, "unable to add child device object (relid %d)",
283                         newChannel->OfferMsg.ChildRelId);
284
285                 SpinlockAcquire(gVmbusConnection.ChannelLock);
286                 REMOVE_ENTRY_LIST(&newChannel->ListEntry);
287                 SpinlockRelease(gVmbusConnection.ChannelLock);
288
289                 FreeVmbusChannel(newChannel);
290         }
291         else
292         {
293                 // This state is used to indicate a successful open so that when we do close the channel normally,
294                 // we can cleanup properly
295                 newChannel->State = CHANNEL_OPEN_STATE;
296         }
297         DPRINT_EXIT(VMBUS);
298 }
299
300 /*++
301
302 Name:
303         VmbusChannelProcessRescindOffer()
304
305 Description:
306         Rescind the offer by initiating a device removal
307
308 --*/
309 static void
310 VmbusChannelProcessRescindOffer(
311         void * context
312         )
313 {
314         VMBUS_CHANNEL* channel=(VMBUS_CHANNEL*)context;
315
316         DPRINT_ENTER(VMBUS);
317
318         VmbusChildDeviceRemove(channel->DeviceObject);
319
320         DPRINT_EXIT(VMBUS);
321 }
322
323
324 /*++
325
326 Name:
327         VmbusChannelOnOffer()
328
329 Description:
330         Handler for channel offers from vmbus in parent partition. We ignore all offers except
331         network and storage offers. For each network and storage offers, we create a channel object
332         and queue a work item to the channel object to process the offer synchronously
333
334 --*/
335 static void
336 VmbusChannelOnOffer(
337         PVMBUS_CHANNEL_MESSAGE_HEADER hdr
338         )
339 {
340         VMBUS_CHANNEL_OFFER_CHANNEL* offer = (VMBUS_CHANNEL_OFFER_CHANNEL*)hdr;
341         VMBUS_CHANNEL* newChannel;
342
343         GUID *guidType;
344         GUID *guidInstance;
345         int i;
346         int fSupported=0;
347
348         DPRINT_ENTER(VMBUS);
349
350         for (i=0; i<MAX_NUM_DEVICE_CLASSES_SUPPORTED; i++)
351         {
352                 if (memcmp(&offer->Offer.InterfaceType, &gSupportedDeviceClasses[i], sizeof(GUID)) == 0)
353                 {
354                         fSupported = 1;
355                         break;
356                 }
357         }
358
359         if (!fSupported)
360         {
361                 DPRINT_DBG(VMBUS, "Ignoring channel offer notification for child relid %d", offer->ChildRelId);
362                 DPRINT_EXIT(VMBUS);
363
364                 return;
365         }
366
367         guidType = &offer->Offer.InterfaceType;
368         guidInstance = &offer->Offer.InterfaceInstance;
369
370         DPRINT_INFO(VMBUS, "Channel offer notification - child relid %d monitor id %d allocated %d, "
371                 "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%02x%02x} "
372                 "instance {%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%02x%02x}",
373                 offer->ChildRelId,
374                 offer->MonitorId,
375                 offer->MonitorAllocated,
376                 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],
377                 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]);
378
379         // Allocate the channel object and save this offer.
380         newChannel = AllocVmbusChannel();
381         if (!newChannel)
382         {
383                 DPRINT_ERR(VMBUS, "unable to allocate channel object");
384                 return;
385         }
386
387         DPRINT_DBG(VMBUS, "channel object allocated - %p", newChannel);
388
389         memcpy(&newChannel->OfferMsg, offer, sizeof(VMBUS_CHANNEL_OFFER_CHANNEL));
390         newChannel->MonitorGroup = (u8)offer->MonitorId / 32;
391         newChannel->MonitorBit = (u8)offer->MonitorId % 32;
392
393         // TODO: Make sure the offer comes from our parent partition
394         WorkQueueQueueWorkItem(newChannel->ControlWQ, VmbusChannelProcessOffer, newChannel);
395
396         DPRINT_EXIT(VMBUS);
397 }
398
399
400 /*++
401
402 Name:
403         VmbusChannelOnOfferRescind()
404
405 Description:
406         Rescind offer handler. We queue a work item to process this offer
407         synchronously
408
409 --*/
410 static void
411 VmbusChannelOnOfferRescind(
412         PVMBUS_CHANNEL_MESSAGE_HEADER hdr
413         )
414 {
415         VMBUS_CHANNEL_RESCIND_OFFER* rescind = (VMBUS_CHANNEL_RESCIND_OFFER*)hdr;
416         VMBUS_CHANNEL* channel;
417
418         DPRINT_ENTER(VMBUS);
419
420         channel = GetChannelFromRelId(rescind->ChildRelId);
421         if (channel == NULL)
422         {
423                 DPRINT_DBG(VMBUS, "channel not found for relId %d", rescind->ChildRelId);
424                 return;
425         }
426
427         WorkQueueQueueWorkItem(channel->ControlWQ, VmbusChannelProcessRescindOffer, channel);
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;
472         VMBUS_CHANNEL_MSGINFO* msgInfo;
473         VMBUS_CHANNEL_MESSAGE_HEADER* requestHeader;
474         VMBUS_CHANNEL_OPEN_CHANNEL* openMsg;
475         unsigned long flags;
476
477         DPRINT_ENTER(VMBUS);
478
479         DPRINT_DBG(VMBUS, "vmbus open result - %d", result->Status);
480
481         // Find the open msg, copy the result and signal/unblock the wait event
482         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
483
484         ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList)
485         {
486                 msgInfo = (VMBUS_CHANNEL_MSGINFO*) curr;
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));
496                                 WaitEventSet(msgInfo->WaitEvent);
497                                 break;
498                         }
499                 }
500         }
501         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
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;
526         VMBUS_CHANNEL_MSGINFO *msgInfo;
527         VMBUS_CHANNEL_MESSAGE_HEADER *requestHeader;
528         VMBUS_CHANNEL_GPADL_HEADER *gpadlHeader;
529         unsigned long flags;
530
531         DPRINT_ENTER(VMBUS);
532
533         DPRINT_DBG(VMBUS, "vmbus gpadl created result - %d", gpadlCreated->CreationStatus);
534
535         // Find the establish msg, copy the result and signal/unblock the wait event
536         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
537
538         ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList)
539         {
540                 msgInfo = (VMBUS_CHANNEL_MSGINFO*) curr;
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));
551                                 WaitEventSet(msgInfo->WaitEvent);
552                                 break;
553                         }
554                 }
555         }
556         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
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;
581         VMBUS_CHANNEL_MSGINFO* msgInfo;
582         VMBUS_CHANNEL_MESSAGE_HEADER *requestHeader;
583         VMBUS_CHANNEL_GPADL_TEARDOWN *gpadlTeardown;
584         unsigned long flags;
585
586         DPRINT_ENTER(VMBUS);
587
588         // Find the open msg, copy the result and signal/unblock the wait event
589         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
590
591         ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList)
592         {
593                 msgInfo = (VMBUS_CHANNEL_MSGINFO*) curr;
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));
603                                 WaitEventSet(msgInfo->WaitEvent);
604                                 break;
605                         }
606                 }
607         }
608         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
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;
632         VMBUS_CHANNEL_MSGINFO *msgInfo;
633         VMBUS_CHANNEL_MESSAGE_HEADER *requestHeader;
634         VMBUS_CHANNEL_INITIATE_CONTACT *initiate;
635         VMBUS_CHANNEL_VERSION_RESPONSE *versionResponse  = (VMBUS_CHANNEL_VERSION_RESPONSE*)hdr;
636         unsigned long flags;
637
638         DPRINT_ENTER(VMBUS);
639
640         spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
641
642         ITERATE_LIST_ENTRIES(anchor, curr, &gVmbusConnection.ChannelMsgList)
643         {
644                 msgInfo = (VMBUS_CHANNEL_MSGINFO*) curr;
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));
651                         WaitEventSet(msgInfo->WaitEvent);
652                 }
653         }
654         spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
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 --*/
670 void
671 VmbusOnChannelMessage(
672         void *Context
673         )
674 {
675         HV_MESSAGE *msg=(HV_MESSAGE*)Context;
676         VMBUS_CHANNEL_MESSAGE_HEADER* hdr;
677         int size;
678
679         DPRINT_ENTER(VMBUS);
680
681         hdr = (VMBUS_CHANNEL_MESSAGE_HEADER*)msg->u.Payload;
682         size=msg->Header.PayloadSize;
683
684         DPRINT_DBG(VMBUS, "message type %d size %d", hdr->MessageType, size);
685
686         if (hdr->MessageType >= ChannelMessageCount)
687         {
688                 DPRINT_ERR(VMBUS, "Received invalid channel message type %d size %d", hdr->MessageType, size);
689                 PrintBytes((unsigned char *)msg->u.Payload, size);
690                 kfree(msg);
691                 return;
692         }
693
694         if (gChannelMessageTable[hdr->MessageType].messageHandler)
695         {
696                 gChannelMessageTable[hdr->MessageType].messageHandler(hdr);
697         }
698         else
699         {
700                 DPRINT_ERR(VMBUS, "Unhandled channel message type %d", hdr->MessageType);
701         }
702
703         // Free the msg that was allocated in VmbusOnMsgDPC()
704         kfree(msg);
705         DPRINT_EXIT(VMBUS);
706 }
707
708
709 /*++
710
711 Name:
712         VmbusChannelRequestOffers()
713
714 Description:
715         Send a request to get all our pending offers.
716
717 --*/
718 int
719 VmbusChannelRequestOffers(
720         void
721         )
722 {
723         int ret=0;
724         VMBUS_CHANNEL_MESSAGE_HEADER* msg;
725         VMBUS_CHANNEL_MSGINFO* msgInfo;
726
727         DPRINT_ENTER(VMBUS);
728
729         msgInfo = kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO) + sizeof(VMBUS_CHANNEL_MESSAGE_HEADER), GFP_KERNEL);
730         ASSERT(msgInfo != NULL);
731
732         msgInfo->WaitEvent = WaitEventCreate();
733         msg = (VMBUS_CHANNEL_MESSAGE_HEADER*)msgInfo->Msg;
734
735         msg->MessageType = ChannelMessageRequestOffers;
736
737         /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
738         INSERT_TAIL_LIST(&gVmbusConnection.channelMsgList, &msgInfo->msgListEntry);
739         SpinlockRelease(gVmbusConnection.channelMsgLock);*/
740
741         ret = VmbusPostMessage(msg, sizeof(VMBUS_CHANNEL_MESSAGE_HEADER));
742         if (ret != 0)
743         {
744                 DPRINT_ERR(VMBUS, "Unable to request offers - %d", ret);
745
746                 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
747                 REMOVE_ENTRY_LIST(&msgInfo->msgListEntry);
748                 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
749
750                 goto Cleanup;
751         }
752         //WaitEventWait(msgInfo->waitEvent);
753
754         /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
755         REMOVE_ENTRY_LIST(&msgInfo->msgListEntry);
756         SpinlockRelease(gVmbusConnection.channelMsgLock);*/
757
758
759 Cleanup:
760         if (msgInfo)
761         {
762                 WaitEventClose(msgInfo->WaitEvent);
763                 kfree(msgInfo);
764         }
765
766         DPRINT_EXIT(VMBUS);
767
768         return ret;
769 }
770
771 /*++
772
773 Name:
774         VmbusChannelReleaseUnattachedChannels()
775
776 Description:
777         Release channels that are unattached/unconnected ie (no drivers associated)
778
779 --*/
780 void
781 VmbusChannelReleaseUnattachedChannels(
782         void
783         )
784 {
785         LIST_ENTRY *entry;
786         VMBUS_CHANNEL *channel;
787         VMBUS_CHANNEL *start=NULL;
788
789         SpinlockAcquire(gVmbusConnection.ChannelLock);
790
791         while (!IsListEmpty(&gVmbusConnection.ChannelList))
792         {
793                 entry = TOP_LIST_ENTRY(&gVmbusConnection.ChannelList);
794                 channel = CONTAINING_RECORD(entry, VMBUS_CHANNEL, ListEntry);
795
796                 if (channel == start)
797                         break;
798
799                 if (!channel->DeviceObject->Driver)
800                 {
801                         REMOVE_ENTRY_LIST(&channel->ListEntry);
802                         DPRINT_INFO(VMBUS, "Releasing unattached device object %p", channel->DeviceObject);
803
804                         VmbusChildDeviceRemove(channel->DeviceObject);
805                         FreeVmbusChannel(channel);
806                 }
807                 else
808                 {
809                         if (!start)
810                         {
811                                 start = channel;
812                         }
813                 }
814         }
815
816         SpinlockRelease(gVmbusConnection.ChannelLock);
817 }
818
819 // eof
820