2 * Copyright (c) 2009, Microsoft Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/list.h>
27 #include "VmbusPrivate.h"
29 struct vmbus_channel_message_table_entry
{
30 enum vmbus_channel_message_type messageType
;
31 void (*messageHandler
)(struct vmbus_channel_message_header
*msg
);
34 #define MAX_NUM_DEVICE_CLASSES_SUPPORTED 4
35 static const struct hv_guid
36 gSupportedDeviceClasses
[MAX_NUM_DEVICE_CLASSES_SUPPORTED
] = {
37 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
41 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
42 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
46 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
50 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
51 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
55 /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
59 0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
60 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A
64 /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
68 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
69 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5
75 * AllocVmbusChannel - Allocate and initialize a vmbus channel object
77 struct vmbus_channel
*AllocVmbusChannel(void)
79 struct vmbus_channel
*channel
;
81 channel
= kzalloc(sizeof(*channel
), GFP_ATOMIC
);
85 spin_lock_init(&channel
->inbound_lock
);
87 init_timer(&channel
->poll_timer
);
88 channel
->poll_timer
.data
= (unsigned long)channel
;
89 channel
->poll_timer
.function
= VmbusChannelOnTimer
;
91 channel
->ControlWQ
= create_workqueue("hv_vmbus_ctl");
92 if (!channel
->ControlWQ
) {
101 * ReleaseVmbusChannel - Release the vmbus channel object itself
103 static inline void ReleaseVmbusChannel(void *context
)
105 struct vmbus_channel
*channel
= context
;
109 DPRINT_DBG(VMBUS
, "releasing channel (%p)", channel
);
110 destroy_workqueue(channel
->ControlWQ
);
111 DPRINT_DBG(VMBUS
, "channel released (%p)", channel
);
119 * FreeVmbusChannel - Release the resources used by the vmbus channel object
121 void FreeVmbusChannel(struct vmbus_channel
*Channel
)
123 del_timer_sync(&Channel
->poll_timer
);
126 * We have to release the channel's workqueue/thread in the vmbus's
127 * workqueue/thread context
128 * ie we can't destroy ourselves.
130 osd_schedule_callback(gVmbusConnection
.WorkQueue
, ReleaseVmbusChannel
,
135 * VmbusChannelProcessOffer - Process the offer by creating a channel/device associated with this offer
137 static void VmbusChannelProcessOffer(void *context
)
139 struct vmbus_channel
*newChannel
= context
;
140 struct vmbus_channel
*channel
;
147 /* Make sure this is a new offer */
148 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
150 list_for_each_entry(channel
, &gVmbusConnection
.ChannelList
, ListEntry
) {
151 if (!memcmp(&channel
->OfferMsg
.Offer
.InterfaceType
,
152 &newChannel
->OfferMsg
.Offer
.InterfaceType
,
153 sizeof(struct hv_guid
)) &&
154 !memcmp(&channel
->OfferMsg
.Offer
.InterfaceInstance
,
155 &newChannel
->OfferMsg
.Offer
.InterfaceInstance
,
156 sizeof(struct hv_guid
))) {
163 list_add_tail(&newChannel
->ListEntry
,
164 &gVmbusConnection
.ChannelList
);
166 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
169 DPRINT_DBG(VMBUS
, "Ignoring duplicate offer for relid (%d)",
170 newChannel
->OfferMsg
.ChildRelId
);
171 FreeVmbusChannel(newChannel
);
177 * Start the process of binding this offer to the driver
178 * We need to set the DeviceObject field before calling
179 * VmbusChildDeviceAdd()
181 newChannel
->DeviceObject
= VmbusChildDeviceCreate(
182 &newChannel
->OfferMsg
.Offer
.InterfaceType
,
183 &newChannel
->OfferMsg
.Offer
.InterfaceInstance
,
186 DPRINT_DBG(VMBUS
, "child device object allocated - %p",
187 newChannel
->DeviceObject
);
190 * Add the new device to the bus. This will kick off device-driver
191 * binding which eventually invokes the device driver's AddDevice()
194 ret
= VmbusChildDeviceAdd(newChannel
->DeviceObject
);
197 "unable to add child device object (relid %d)",
198 newChannel
->OfferMsg
.ChildRelId
);
200 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
201 list_del(&newChannel
->ListEntry
);
202 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
204 FreeVmbusChannel(newChannel
);
207 * This state is used to indicate a successful open
208 * so that when we do close the channel normally, we
209 * can cleanup properly
211 newChannel
->State
= CHANNEL_OPEN_STATE
;
217 * VmbusChannelProcessRescindOffer - Rescind the offer by initiating a device removal
219 static void VmbusChannelProcessRescindOffer(void *context
)
221 struct vmbus_channel
*channel
= context
;
224 VmbusChildDeviceRemove(channel
->DeviceObject
);
229 * VmbusChannelOnOffer - Handler for channel offers from vmbus in parent partition.
231 * We ignore all offers except network and storage offers. For each network and
232 * storage offers, we create a channel object and queue a work item to the
233 * channel object to process the offer synchronously
235 static void VmbusChannelOnOffer(struct vmbus_channel_message_header
*hdr
)
237 struct vmbus_channel_offer_channel
*offer
;
238 struct vmbus_channel
*newChannel
;
239 struct hv_guid
*guidType
;
240 struct hv_guid
*guidInstance
;
246 offer
= (struct vmbus_channel_offer_channel
*)hdr
;
247 for (i
= 0; i
< MAX_NUM_DEVICE_CLASSES_SUPPORTED
; i
++) {
248 if (memcmp(&offer
->Offer
.InterfaceType
,
249 &gSupportedDeviceClasses
[i
], sizeof(struct hv_guid
)) == 0) {
256 DPRINT_DBG(VMBUS
, "Ignoring channel offer notification for "
257 "child relid %d", offer
->ChildRelId
);
262 guidType
= &offer
->Offer
.InterfaceType
;
263 guidInstance
= &offer
->Offer
.InterfaceInstance
;
265 DPRINT_INFO(VMBUS
, "Channel offer notification - "
266 "child relid %d monitor id %d allocated %d, "
267 "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
268 "%02x%02x%02x%02x%02x%02x%02x%02x} "
269 "instance {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
270 "%02x%02x%02x%02x%02x%02x%02x%02x}",
271 offer
->ChildRelId
, offer
->MonitorId
,
272 offer
->MonitorAllocated
,
273 guidType
->data
[3], guidType
->data
[2],
274 guidType
->data
[1], guidType
->data
[0],
275 guidType
->data
[5], guidType
->data
[4],
276 guidType
->data
[7], guidType
->data
[6],
277 guidType
->data
[8], guidType
->data
[9],
278 guidType
->data
[10], guidType
->data
[11],
279 guidType
->data
[12], guidType
->data
[13],
280 guidType
->data
[14], guidType
->data
[15],
281 guidInstance
->data
[3], guidInstance
->data
[2],
282 guidInstance
->data
[1], guidInstance
->data
[0],
283 guidInstance
->data
[5], guidInstance
->data
[4],
284 guidInstance
->data
[7], guidInstance
->data
[6],
285 guidInstance
->data
[8], guidInstance
->data
[9],
286 guidInstance
->data
[10], guidInstance
->data
[11],
287 guidInstance
->data
[12], guidInstance
->data
[13],
288 guidInstance
->data
[14], guidInstance
->data
[15]);
290 /* Allocate the channel object and save this offer. */
291 newChannel
= AllocVmbusChannel();
293 DPRINT_ERR(VMBUS
, "unable to allocate channel object");
297 DPRINT_DBG(VMBUS
, "channel object allocated - %p", newChannel
);
299 memcpy(&newChannel
->OfferMsg
, offer
,
300 sizeof(struct vmbus_channel_offer_channel
));
301 newChannel
->MonitorGroup
= (u8
)offer
->MonitorId
/ 32;
302 newChannel
->MonitorBit
= (u8
)offer
->MonitorId
% 32;
304 /* TODO: Make sure the offer comes from our parent partition */
305 osd_schedule_callback(newChannel
->ControlWQ
, VmbusChannelProcessOffer
,
312 * VmbusChannelOnOfferRescind - Rescind offer handler.
314 * We queue a work item to process this offer synchronously
316 static void VmbusChannelOnOfferRescind(struct vmbus_channel_message_header
*hdr
)
318 struct vmbus_channel_rescind_offer
*rescind
;
319 struct vmbus_channel
*channel
;
323 rescind
= (struct vmbus_channel_rescind_offer
*)hdr
;
324 channel
= GetChannelFromRelId(rescind
->ChildRelId
);
325 if (channel
== NULL
) {
326 DPRINT_DBG(VMBUS
, "channel not found for relId %d",
327 rescind
->ChildRelId
);
331 osd_schedule_callback(channel
->ControlWQ
,
332 VmbusChannelProcessRescindOffer
,
339 * VmbusChannelOnOffersDelivered - This is invoked when all offers have been delivered.
341 * Nothing to do here.
343 static void VmbusChannelOnOffersDelivered(
344 struct vmbus_channel_message_header
*hdr
)
351 * VmbusChannelOnOpenResult - Open result handler.
353 * This is invoked when we received a response to our channel open request.
354 * Find the matching request, copy the response and signal the requesting
357 static void VmbusChannelOnOpenResult(struct vmbus_channel_message_header
*hdr
)
359 struct vmbus_channel_open_result
*result
;
360 struct list_head
*curr
;
361 struct vmbus_channel_msginfo
*msgInfo
;
362 struct vmbus_channel_message_header
*requestHeader
;
363 struct vmbus_channel_open_channel
*openMsg
;
368 result
= (struct vmbus_channel_open_result
*)hdr
;
369 DPRINT_DBG(VMBUS
, "vmbus open result - %d", result
->Status
);
372 * Find the open msg, copy the result and signal/unblock the wait event
374 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
376 list_for_each(curr
, &gVmbusConnection
.ChannelMsgList
) {
377 /* FIXME: this should probably use list_entry() instead */
378 msgInfo
= (struct vmbus_channel_msginfo
*)curr
;
379 requestHeader
= (struct vmbus_channel_message_header
*)msgInfo
->Msg
;
381 if (requestHeader
->MessageType
== ChannelMessageOpenChannel
) {
382 openMsg
= (struct vmbus_channel_open_channel
*)msgInfo
->Msg
;
383 if (openMsg
->ChildRelId
== result
->ChildRelId
&&
384 openMsg
->OpenId
== result
->OpenId
) {
385 memcpy(&msgInfo
->Response
.OpenResult
,
387 sizeof(struct vmbus_channel_open_result
));
388 osd_WaitEventSet(msgInfo
->WaitEvent
);
393 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
399 * VmbusChannelOnGpadlCreated - GPADL created handler.
401 * This is invoked when we received a response to our gpadl create request.
402 * Find the matching request, copy the response and signal the requesting
405 static void VmbusChannelOnGpadlCreated(struct vmbus_channel_message_header
*hdr
)
407 struct vmbus_channel_gpadl_created
*gpadlCreated
;
408 struct list_head
*curr
;
409 struct vmbus_channel_msginfo
*msgInfo
;
410 struct vmbus_channel_message_header
*requestHeader
;
411 struct vmbus_channel_gpadl_header
*gpadlHeader
;
416 gpadlCreated
= (struct vmbus_channel_gpadl_created
*)hdr
;
417 DPRINT_DBG(VMBUS
, "vmbus gpadl created result - %d",
418 gpadlCreated
->CreationStatus
);
421 * Find the establish msg, copy the result and signal/unblock the wait
424 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
426 list_for_each(curr
, &gVmbusConnection
.ChannelMsgList
) {
427 /* FIXME: this should probably use list_entry() instead */
428 msgInfo
= (struct vmbus_channel_msginfo
*)curr
;
429 requestHeader
= (struct vmbus_channel_message_header
*)msgInfo
->Msg
;
431 if (requestHeader
->MessageType
== ChannelMessageGpadlHeader
) {
432 gpadlHeader
= (struct vmbus_channel_gpadl_header
*)requestHeader
;
434 if ((gpadlCreated
->ChildRelId
==
435 gpadlHeader
->ChildRelId
) &&
436 (gpadlCreated
->Gpadl
== gpadlHeader
->Gpadl
)) {
437 memcpy(&msgInfo
->Response
.GpadlCreated
,
439 sizeof(struct vmbus_channel_gpadl_created
));
440 osd_WaitEventSet(msgInfo
->WaitEvent
);
445 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
451 * VmbusChannelOnGpadlTorndown - GPADL torndown handler.
453 * This is invoked when we received a response to our gpadl teardown request.
454 * Find the matching request, copy the response and signal the requesting
457 static void VmbusChannelOnGpadlTorndown(
458 struct vmbus_channel_message_header
*hdr
)
460 struct vmbus_channel_gpadl_torndown
*gpadlTorndown
;
461 struct list_head
*curr
;
462 struct vmbus_channel_msginfo
*msgInfo
;
463 struct vmbus_channel_message_header
*requestHeader
;
464 struct vmbus_channel_gpadl_teardown
*gpadlTeardown
;
469 gpadlTorndown
= (struct vmbus_channel_gpadl_torndown
*)hdr
;
472 * Find the open msg, copy the result and signal/unblock the wait event
474 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
476 list_for_each(curr
, &gVmbusConnection
.ChannelMsgList
) {
477 /* FIXME: this should probably use list_entry() instead */
478 msgInfo
= (struct vmbus_channel_msginfo
*)curr
;
479 requestHeader
= (struct vmbus_channel_message_header
*)msgInfo
->Msg
;
481 if (requestHeader
->MessageType
== ChannelMessageGpadlTeardown
) {
482 gpadlTeardown
= (struct vmbus_channel_gpadl_teardown
*)requestHeader
;
484 if (gpadlTorndown
->Gpadl
== gpadlTeardown
->Gpadl
) {
485 memcpy(&msgInfo
->Response
.GpadlTorndown
,
487 sizeof(struct vmbus_channel_gpadl_torndown
));
488 osd_WaitEventSet(msgInfo
->WaitEvent
);
493 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
499 * VmbusChannelOnVersionResponse - Version response handler
501 * This is invoked when we received a response to our initiate contact request.
502 * Find the matching request, copy the response and signal the requesting
505 static void VmbusChannelOnVersionResponse(
506 struct vmbus_channel_message_header
*hdr
)
508 struct list_head
*curr
;
509 struct vmbus_channel_msginfo
*msgInfo
;
510 struct vmbus_channel_message_header
*requestHeader
;
511 struct vmbus_channel_initiate_contact
*initiate
;
512 struct vmbus_channel_version_response
*versionResponse
;
517 versionResponse
= (struct vmbus_channel_version_response
*)hdr
;
518 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
520 list_for_each(curr
, &gVmbusConnection
.ChannelMsgList
) {
521 /* FIXME: this should probably use list_entry() instead */
522 msgInfo
= (struct vmbus_channel_msginfo
*)curr
;
523 requestHeader
= (struct vmbus_channel_message_header
*)msgInfo
->Msg
;
525 if (requestHeader
->MessageType
==
526 ChannelMessageInitiateContact
) {
527 initiate
= (struct vmbus_channel_initiate_contact
*)requestHeader
;
528 memcpy(&msgInfo
->Response
.VersionResponse
,
530 sizeof(struct vmbus_channel_version_response
));
531 osd_WaitEventSet(msgInfo
->WaitEvent
);
534 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
539 /* Channel message dispatch table */
540 static struct vmbus_channel_message_table_entry
541 gChannelMessageTable
[ChannelMessageCount
] = {
542 {ChannelMessageInvalid
, NULL
},
543 {ChannelMessageOfferChannel
, VmbusChannelOnOffer
},
544 {ChannelMessageRescindChannelOffer
, VmbusChannelOnOfferRescind
},
545 {ChannelMessageRequestOffers
, NULL
},
546 {ChannelMessageAllOffersDelivered
, VmbusChannelOnOffersDelivered
},
547 {ChannelMessageOpenChannel
, NULL
},
548 {ChannelMessageOpenChannelResult
, VmbusChannelOnOpenResult
},
549 {ChannelMessageCloseChannel
, NULL
},
550 {ChannelMessageGpadlHeader
, NULL
},
551 {ChannelMessageGpadlBody
, NULL
},
552 {ChannelMessageGpadlCreated
, VmbusChannelOnGpadlCreated
},
553 {ChannelMessageGpadlTeardown
, NULL
},
554 {ChannelMessageGpadlTorndown
, VmbusChannelOnGpadlTorndown
},
555 {ChannelMessageRelIdReleased
, NULL
},
556 {ChannelMessageInitiateContact
, NULL
},
557 {ChannelMessageVersionResponse
, VmbusChannelOnVersionResponse
},
558 {ChannelMessageUnload
, NULL
},
562 * VmbusOnChannelMessage - Handler for channel protocol messages.
564 * This is invoked in the vmbus worker thread context.
566 void VmbusOnChannelMessage(void *Context
)
568 struct hv_message
*msg
= Context
;
569 struct vmbus_channel_message_header
*hdr
;
574 hdr
= (struct vmbus_channel_message_header
*)msg
->u
.Payload
;
575 size
= msg
->Header
.PayloadSize
;
577 DPRINT_DBG(VMBUS
, "message type %d size %d", hdr
->MessageType
, size
);
579 if (hdr
->MessageType
>= ChannelMessageCount
) {
581 "Received invalid channel message type %d size %d",
582 hdr
->MessageType
, size
);
583 print_hex_dump_bytes("", DUMP_PREFIX_NONE
,
584 (unsigned char *)msg
->u
.Payload
, size
);
589 if (gChannelMessageTable
[hdr
->MessageType
].messageHandler
)
590 gChannelMessageTable
[hdr
->MessageType
].messageHandler(hdr
);
592 DPRINT_ERR(VMBUS
, "Unhandled channel message type %d",
595 /* Free the msg that was allocated in VmbusOnMsgDPC() */
601 * VmbusChannelRequestOffers - Send a request to get all our pending offers.
603 int VmbusChannelRequestOffers(void)
605 struct vmbus_channel_message_header
*msg
;
606 struct vmbus_channel_msginfo
*msgInfo
;
611 msgInfo
= kmalloc(sizeof(*msgInfo
) +
612 sizeof(struct vmbus_channel_message_header
),
614 ASSERT(msgInfo
!= NULL
);
616 msgInfo
->WaitEvent
= osd_WaitEventCreate();
617 msg
= (struct vmbus_channel_message_header
*)msgInfo
->Msg
;
619 msg
->MessageType
= ChannelMessageRequestOffers
;
621 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
622 INSERT_TAIL_LIST(&gVmbusConnection.channelMsgList,
623 &msgInfo->msgListEntry);
624 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
626 ret
= VmbusPostMessage(msg
,
627 sizeof(struct vmbus_channel_message_header
));
629 DPRINT_ERR(VMBUS
, "Unable to request offers - %d", ret
);
631 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
632 REMOVE_ENTRY_LIST(&msgInfo->msgListEntry);
633 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
637 /* osd_WaitEventWait(msgInfo->waitEvent); */
639 /*SpinlockAcquire(gVmbusConnection.channelMsgLock);
640 REMOVE_ENTRY_LIST(&msgInfo->msgListEntry);
641 SpinlockRelease(gVmbusConnection.channelMsgLock);*/
646 kfree(msgInfo
->WaitEvent
);
655 * VmbusChannelReleaseUnattachedChannels - Release channels that are unattached/unconnected ie (no drivers associated)
657 void VmbusChannelReleaseUnattachedChannels(void)
659 struct vmbus_channel
*channel
, *pos
;
660 struct vmbus_channel
*start
= NULL
;
663 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
665 list_for_each_entry_safe(channel
, pos
, &gVmbusConnection
.ChannelList
,
667 if (channel
== start
)
670 if (!channel
->DeviceObject
->Driver
) {
671 list_del(&channel
->ListEntry
);
673 "Releasing unattached device object %p",
674 channel
->DeviceObject
);
676 VmbusChildDeviceRemove(channel
->DeviceObject
);
677 FreeVmbusChannel(channel
);
684 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);