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