3 * Copyright (c) 2009, Microsoft Corporation.
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.
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
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.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/kernel.h>
25 #include "include/osd.h"
26 #include "include/logging.h"
28 #include "VmbusPrivate.h"
30 /* Internal routines */
32 VmbusChannelCreateGpadlHeader(
33 void * Kbuffer
, /* must be phys and virt contiguous */
34 u32 Size
, /* page-size multiple */
35 VMBUS_CHANNEL_MSGINFO
**msgInfo
,
41 VMBUS_CHANNEL
*Channel
47 VMBUS_CHANNEL
*Channel
54 HV_MONITOR_PAGE
*MonitorPage
60 DPRINT_DBG(VMBUS
, "monitorPage - %p, trigger state - %d", MonitorPage
, MonitorPage
->TriggerState
);
64 DPRINT_DBG(VMBUS
, "trigger group (%d) - %llx", i
, MonitorPage
->TriggerGroup
[i
].AsUINT64
);
71 DPRINT_DBG(VMBUS
, "latency (%d)(%d) - %llx", i
, j
, MonitorPage
->Latency
[i
][j
]);
78 DPRINT_DBG(VMBUS
, "param-conn id (%d)(%d) - %d", i
, j
, MonitorPage
->Parameter
[i
][j
].ConnectionId
.Asu32
);
79 DPRINT_DBG(VMBUS
, "param-flag (%d)(%d) - %d", i
, j
, MonitorPage
->Parameter
[i
][j
].FlagNumber
);
89 VmbusChannelSetEvent()
92 Trigger an event notification on the specified channel.
97 VMBUS_CHANNEL
*Channel
100 HV_MONITOR_PAGE
*monitorPage
;
104 if (Channel
->OfferMsg
.MonitorAllocated
)
106 /* Each u32 represents 32 channels */
107 set_bit(Channel
->OfferMsg
.ChildRelId
& 31,
108 (unsigned long *) gVmbusConnection
.SendInterruptPage
+
109 (Channel
->OfferMsg
.ChildRelId
>> 5) );
111 monitorPage
= (HV_MONITOR_PAGE
*)gVmbusConnection
.MonitorPages
;
112 monitorPage
++; /* Get the child to parent monitor page */
114 set_bit(Channel
->MonitorBit
,
115 (unsigned long *) &monitorPage
->TriggerGroup
[Channel
->MonitorGroup
].Pending
);
120 VmbusSetEvent(Channel
->OfferMsg
.ChildRelId
);
128 VmbusChannelClearEvent(
129 VMBUS_CHANNEL
*Channel
132 HV_MONITOR_PAGE
*monitorPage
;
136 if (Channel
->OfferMsg
.MonitorAllocated
)
138 /* Each u32 represents 32 channels */
139 clear_bit(Channel
->OfferMsg
.ChildRelId
& 31,
140 (unsigned long *) gVmbusConnection
.SendInterruptPage
+ (Channel
->OfferMsg
.ChildRelId
>> 5));
142 monitorPage
= (HV_MONITOR_PAGE
*)gVmbusConnection
.MonitorPages
;
143 monitorPage
++; /* Get the child to parent monitor page */
145 clear_bit(Channel
->MonitorBit
,
146 (unsigned long *) &monitorPage
->TriggerGroup
[Channel
->MonitorGroup
].Pending
);
156 VmbusChannelGetDebugInfo()
159 Retrieve various channel debug info
163 VmbusChannelGetDebugInfo(
164 VMBUS_CHANNEL
*Channel
,
165 VMBUS_CHANNEL_DEBUG_INFO
*DebugInfo
168 HV_MONITOR_PAGE
*monitorPage
;
169 u8 monitorGroup
= (u8
)Channel
->OfferMsg
.MonitorId
/ 32;
170 u8 monitorOffset
= (u8
)Channel
->OfferMsg
.MonitorId
% 32;
171 /* u32 monitorBit = 1 << monitorOffset; */
173 DebugInfo
->RelId
= Channel
->OfferMsg
.ChildRelId
;
174 DebugInfo
->State
= Channel
->State
;
175 memcpy(&DebugInfo
->InterfaceType
, &Channel
->OfferMsg
.Offer
.InterfaceType
, sizeof(GUID
));
176 memcpy(&DebugInfo
->InterfaceInstance
, &Channel
->OfferMsg
.Offer
.InterfaceInstance
, sizeof(GUID
));
178 monitorPage
= (HV_MONITOR_PAGE
*)gVmbusConnection
.MonitorPages
;
180 DebugInfo
->MonitorId
= Channel
->OfferMsg
.MonitorId
;
182 DebugInfo
->ServerMonitorPending
= monitorPage
->TriggerGroup
[monitorGroup
].Pending
;
183 DebugInfo
->ServerMonitorLatency
= monitorPage
->Latency
[monitorGroup
][ monitorOffset
];
184 DebugInfo
->ServerMonitorConnectionId
= monitorPage
->Parameter
[monitorGroup
][ monitorOffset
].ConnectionId
.u
.Id
;
188 DebugInfo
->ClientMonitorPending
= monitorPage
->TriggerGroup
[monitorGroup
].Pending
;
189 DebugInfo
->ClientMonitorLatency
= monitorPage
->Latency
[monitorGroup
][ monitorOffset
];
190 DebugInfo
->ClientMonitorConnectionId
= monitorPage
->Parameter
[monitorGroup
][ monitorOffset
].ConnectionId
.u
.Id
;
192 RingBufferGetDebugInfo(&Channel
->Inbound
, &DebugInfo
->Inbound
);
193 RingBufferGetDebugInfo(&Channel
->Outbound
, &DebugInfo
->Outbound
);
203 Open the specified channel.
208 VMBUS_CHANNEL
*NewChannel
,
209 u32 SendRingBufferSize
,
210 u32 RecvRingBufferSize
,
213 PFN_CHANNEL_CALLBACK pfnOnChannelCallback
,
218 VMBUS_CHANNEL_OPEN_CHANNEL
* openMsg
;
219 VMBUS_CHANNEL_MSGINFO
* openInfo
;
225 /* Aligned to page size */
226 ASSERT(!(SendRingBufferSize
& (PAGE_SIZE
-1)));
227 ASSERT(!(RecvRingBufferSize
& (PAGE_SIZE
-1)));
229 NewChannel
->OnChannelCallback
= pfnOnChannelCallback
;
230 NewChannel
->ChannelCallbackContext
= Context
;
232 /* Allocate the ring buffer */
233 out
= PageAlloc((SendRingBufferSize
+ RecvRingBufferSize
) >> PAGE_SHIFT
);
234 /* out = kzalloc(sendRingBufferSize + recvRingBufferSize, GFP_KERNEL); */
236 ASSERT(((unsigned long)out
& (PAGE_SIZE
-1)) == 0);
238 in
= (void*)((unsigned long)out
+ SendRingBufferSize
);
240 NewChannel
->RingBufferPages
= out
;
241 NewChannel
->RingBufferPageCount
= (SendRingBufferSize
+ RecvRingBufferSize
) >> PAGE_SHIFT
;
243 RingBufferInit(&NewChannel
->Outbound
, out
, SendRingBufferSize
);
245 RingBufferInit(&NewChannel
->Inbound
, in
, RecvRingBufferSize
);
247 /* Establish the gpadl for the ring buffer */
248 DPRINT_DBG(VMBUS
, "Establishing ring buffer's gpadl for channel %p...", NewChannel
);
250 NewChannel
->RingBufferGpadlHandle
= 0;
252 ret
= VmbusChannelEstablishGpadl(NewChannel
,
253 NewChannel
->Outbound
.RingBuffer
,
254 SendRingBufferSize
+ RecvRingBufferSize
,
255 &NewChannel
->RingBufferGpadlHandle
);
257 DPRINT_DBG(VMBUS
, "channel %p <relid %d gpadl 0x%x send ring %p size %d recv ring %p size %d, downstreamoffset %d>",
259 NewChannel
->OfferMsg
.ChildRelId
,
260 NewChannel
->RingBufferGpadlHandle
,
261 NewChannel
->Outbound
.RingBuffer
,
262 NewChannel
->Outbound
.RingSize
,
263 NewChannel
->Inbound
.RingBuffer
,
264 NewChannel
->Inbound
.RingSize
,
267 /* Create and init the channel open message */
268 openInfo
= kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_OPEN_CHANNEL
), GFP_KERNEL
);
269 ASSERT(openInfo
!= NULL
);
271 openInfo
->WaitEvent
= WaitEventCreate();
273 openMsg
= (VMBUS_CHANNEL_OPEN_CHANNEL
*)openInfo
->Msg
;
274 openMsg
->Header
.MessageType
= ChannelMessageOpenChannel
;
275 openMsg
->OpenId
= NewChannel
->OfferMsg
.ChildRelId
; /* FIXME */
276 openMsg
->ChildRelId
= NewChannel
->OfferMsg
.ChildRelId
;
277 openMsg
->RingBufferGpadlHandle
= NewChannel
->RingBufferGpadlHandle
;
278 ASSERT(openMsg
->RingBufferGpadlHandle
);
279 openMsg
->DownstreamRingBufferPageOffset
= SendRingBufferSize
>> PAGE_SHIFT
;
280 openMsg
->ServerContextAreaGpadlHandle
= 0; /* TODO */
282 ASSERT(UserDataLen
<= MAX_USER_DEFINED_BYTES
);
285 memcpy(openMsg
->UserData
, UserData
, UserDataLen
);
288 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
289 INSERT_TAIL_LIST(&gVmbusConnection
.ChannelMsgList
, &openInfo
->MsgListEntry
);
290 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
292 DPRINT_DBG(VMBUS
, "Sending channel open msg...");
294 ret
= VmbusPostMessage(openMsg
, sizeof(VMBUS_CHANNEL_OPEN_CHANNEL
));
297 DPRINT_ERR(VMBUS
, "unable to open channel - %d", ret
);
301 /* FIXME: Need to time-out here */
302 WaitEventWait(openInfo
->WaitEvent
);
304 if (openInfo
->Response
.OpenResult
.Status
== 0)
306 DPRINT_INFO(VMBUS
, "channel <%p> open success!!", NewChannel
);
310 DPRINT_INFO(VMBUS
, "channel <%p> open failed - %d!!", NewChannel
, openInfo
->Response
.OpenResult
.Status
);
314 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
315 REMOVE_ENTRY_LIST(&openInfo
->MsgListEntry
);
316 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
318 kfree(openInfo
->WaitEvent
);
332 Dump the gpadl body message to the console for debugging purposes.
335 static void DumpGpadlBody(
336 VMBUS_CHANNEL_GPADL_BODY
*Gpadl
,
342 pfnCount
= (Len
- sizeof(VMBUS_CHANNEL_GPADL_BODY
))/ sizeof(u64
);
343 DPRINT_DBG(VMBUS
, "gpadl body - len %d pfn count %d", Len
, pfnCount
);
345 for (i
=0; i
< pfnCount
; i
++)
347 DPRINT_DBG(VMBUS
, "gpadl body - %d) pfn %llu", i
, Gpadl
->Pfn
[i
]);
358 Dump the gpadl header message to the console for debugging purposes.
361 static void DumpGpadlHeader(
362 VMBUS_CHANNEL_GPADL_HEADER
*Gpadl
369 DPRINT_DBG(VMBUS
, "gpadl header - relid %d, range count %d, range buflen %d",
373 for (i
=0; i
< Gpadl
->RangeCount
; i
++)
375 pageCount
= Gpadl
->Range
[i
].ByteCount
>> PAGE_SHIFT
;
376 pageCount
= (pageCount
> 26)? 26 : pageCount
;
378 DPRINT_DBG(VMBUS
, "gpadl range %d - len %d offset %d page count %d",
379 i
, Gpadl
->Range
[i
].ByteCount
, Gpadl
->Range
[i
].ByteOffset
, pageCount
);
381 for (j
=0; j
< pageCount
; j
++)
383 DPRINT_DBG(VMBUS
, "%d) pfn %llu", j
, Gpadl
->Range
[i
].PfnArray
[j
]);
391 VmbusChannelCreateGpadlHeader()
394 Creates a gpadl for the specified buffer
398 VmbusChannelCreateGpadlHeader(
399 void * Kbuffer
, /* from kmalloc() */
400 u32 Size
, /* page-size multiple */
401 VMBUS_CHANNEL_MSGINFO
**MsgInfo
,
406 unsigned long long pfn
;
407 VMBUS_CHANNEL_GPADL_HEADER
* gpaHeader
;
408 VMBUS_CHANNEL_GPADL_BODY
* gpadlBody
;
409 VMBUS_CHANNEL_MSGINFO
* msgHeader
;
410 VMBUS_CHANNEL_MSGINFO
* msgBody
;
413 int pfnSum
, pfnCount
, pfnLeft
, pfnCurr
, pfnSize
;
415 /* ASSERT( (kbuffer & (PAGE_SIZE-1)) == 0); */
416 ASSERT( (Size
& (PAGE_SIZE
-1)) == 0);
418 pageCount
= Size
>> PAGE_SHIFT
;
419 pfn
= GetPhysicalAddress(Kbuffer
) >> PAGE_SHIFT
;
421 /* do we need a gpadl body msg */
422 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
- sizeof(VMBUS_CHANNEL_GPADL_HEADER
) - sizeof(GPA_RANGE
);
423 pfnCount
= pfnSize
/ sizeof(u64
);
425 if (pageCount
> pfnCount
) /* we need a gpadl body */
427 /* fill in the header */
428 msgSize
= sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_GPADL_HEADER
) + sizeof(GPA_RANGE
) + pfnCount
*sizeof(u64
);
429 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
431 INITIALIZE_LIST_HEAD(&msgHeader
->SubMsgList
);
432 msgHeader
->MessageSize
=msgSize
;
434 gpaHeader
= (VMBUS_CHANNEL_GPADL_HEADER
*)msgHeader
->Msg
;
435 gpaHeader
->RangeCount
= 1;
436 gpaHeader
->RangeBufLen
= sizeof(GPA_RANGE
) + pageCount
*sizeof(u64
);
437 gpaHeader
->Range
[0].ByteOffset
= 0;
438 gpaHeader
->Range
[0].ByteCount
= Size
;
439 for (i
=0; i
<pfnCount
; i
++)
441 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
443 *MsgInfo
= msgHeader
;
447 pfnLeft
= pageCount
- pfnCount
;
449 /* how many pfns can we fit */
450 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
- sizeof(VMBUS_CHANNEL_GPADL_BODY
);
451 pfnCount
= pfnSize
/ sizeof(u64
);
453 /* fill in the body */
456 if (pfnLeft
> pfnCount
)
465 msgSize
= sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_GPADL_BODY
) + pfnCurr
*sizeof(u64
);
466 msgBody
= kzalloc(msgSize
, GFP_KERNEL
);
468 msgBody
->MessageSize
= msgSize
;
470 gpadlBody
= (VMBUS_CHANNEL_GPADL_BODY
*)msgBody
->Msg
;
472 /* FIXME: Gpadl is u32 and we are using a pointer which could be 64-bit */
473 /* gpadlBody->Gpadl = kbuffer; */
474 for (i
=0; i
<pfnCurr
; i
++)
476 gpadlBody
->Pfn
[i
] = pfn
+ pfnSum
+ i
;
479 /* add to msg header */
480 INSERT_TAIL_LIST(&msgHeader
->SubMsgList
, &msgBody
->MsgListEntry
);
487 /* everything fits in a header */
488 msgSize
= sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_GPADL_HEADER
) + sizeof(GPA_RANGE
) + pageCount
*sizeof(u64
);
489 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
490 msgHeader
->MessageSize
=msgSize
;
492 gpaHeader
= (VMBUS_CHANNEL_GPADL_HEADER
*)msgHeader
->Msg
;
493 gpaHeader
->RangeCount
= 1;
494 gpaHeader
->RangeBufLen
= sizeof(GPA_RANGE
) + pageCount
*sizeof(u64
);
495 gpaHeader
->Range
[0].ByteOffset
= 0;
496 gpaHeader
->Range
[0].ByteCount
= Size
;
497 for (i
=0; i
<pageCount
; i
++)
499 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
502 *MsgInfo
= msgHeader
;
513 VmbusChannelEstablishGpadl()
516 Estabish a GPADL for the specified buffer
520 VmbusChannelEstablishGpadl(
521 VMBUS_CHANNEL
*Channel
,
522 void * Kbuffer
, /* from kmalloc() */
523 u32 Size
, /* page-size multiple */
528 VMBUS_CHANNEL_GPADL_HEADER
* gpadlMsg
;
529 VMBUS_CHANNEL_GPADL_BODY
* gpadlBody
;
530 /* VMBUS_CHANNEL_GPADL_CREATED* gpadlCreated; */
532 VMBUS_CHANNEL_MSGINFO
*msgInfo
;
533 VMBUS_CHANNEL_MSGINFO
*subMsgInfo
;
543 nextGpadlHandle
= atomic_read(&gVmbusConnection
.NextGpadlHandle
);
544 atomic_inc(&gVmbusConnection
.NextGpadlHandle
);
546 VmbusChannelCreateGpadlHeader(Kbuffer
, Size
, &msgInfo
, &msgCount
);
547 ASSERT(msgInfo
!= NULL
);
550 msgInfo
->WaitEvent
= WaitEventCreate();
551 gpadlMsg
= (VMBUS_CHANNEL_GPADL_HEADER
*)msgInfo
->Msg
;
552 gpadlMsg
->Header
.MessageType
= ChannelMessageGpadlHeader
;
553 gpadlMsg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
554 gpadlMsg
->Gpadl
= nextGpadlHandle
;
556 DumpGpadlHeader(gpadlMsg
);
558 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
559 INSERT_TAIL_LIST(&gVmbusConnection
.ChannelMsgList
, &msgInfo
->MsgListEntry
);
560 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
562 DPRINT_DBG(VMBUS
, "buffer %p, size %d msg cnt %d", Kbuffer
, Size
, msgCount
);
564 DPRINT_DBG(VMBUS
, "Sending GPADL Header - len %zd", msgInfo
->MessageSize
- sizeof(VMBUS_CHANNEL_MSGINFO
));
566 ret
= VmbusPostMessage(gpadlMsg
, msgInfo
->MessageSize
- sizeof(VMBUS_CHANNEL_MSGINFO
));
569 DPRINT_ERR(VMBUS
, "Unable to open channel - %d", ret
);
575 ITERATE_LIST_ENTRIES(anchor
, curr
, &msgInfo
->SubMsgList
)
577 subMsgInfo
= (VMBUS_CHANNEL_MSGINFO
*) curr
;
578 gpadlBody
= (VMBUS_CHANNEL_GPADL_BODY
*)subMsgInfo
->Msg
;
580 gpadlBody
->Header
.MessageType
= ChannelMessageGpadlBody
;
581 gpadlBody
->Gpadl
= nextGpadlHandle
;
583 DPRINT_DBG(VMBUS
, "Sending GPADL Body - len %zd", subMsgInfo
->MessageSize
- sizeof(VMBUS_CHANNEL_MSGINFO
));
585 DumpGpadlBody(gpadlBody
, subMsgInfo
->MessageSize
- sizeof(VMBUS_CHANNEL_MSGINFO
));
586 ret
= VmbusPostMessage(gpadlBody
, subMsgInfo
->MessageSize
- sizeof(VMBUS_CHANNEL_MSGINFO
));
590 WaitEventWait(msgInfo
->WaitEvent
);
592 /* At this point, we received the gpadl created msg */
593 DPRINT_DBG(VMBUS
, "Received GPADL created (relid %d, status %d handle %x)",
594 Channel
->OfferMsg
.ChildRelId
,
595 msgInfo
->Response
.GpadlCreated
.CreationStatus
,
598 *GpadlHandle
= gpadlMsg
->Gpadl
;
601 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
602 REMOVE_ENTRY_LIST(&msgInfo
->MsgListEntry
);
603 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
605 kfree(msgInfo
->WaitEvent
);
618 VmbusChannelTeardownGpadl()
621 Teardown the specified GPADL handle
625 VmbusChannelTeardownGpadl(
626 VMBUS_CHANNEL
*Channel
,
631 VMBUS_CHANNEL_GPADL_TEARDOWN
*msg
;
632 VMBUS_CHANNEL_MSGINFO
* info
;
637 ASSERT(GpadlHandle
!= 0);
639 info
= kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN
), GFP_KERNEL
);
640 ASSERT(info
!= NULL
);
642 info
->WaitEvent
= WaitEventCreate();
644 msg
= (VMBUS_CHANNEL_GPADL_TEARDOWN
*)info
->Msg
;
646 msg
->Header
.MessageType
= ChannelMessageGpadlTeardown
;
647 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
648 msg
->Gpadl
= GpadlHandle
;
650 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
651 INSERT_TAIL_LIST(&gVmbusConnection
.ChannelMsgList
, &info
->MsgListEntry
);
652 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
654 ret
= VmbusPostMessage(msg
, sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN
));
660 WaitEventWait(info
->WaitEvent
);
662 /* Received a torndown response */
663 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
664 REMOVE_ENTRY_LIST(&info
->MsgListEntry
);
665 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
667 kfree(info
->WaitEvent
);
682 Close the specified channel
687 VMBUS_CHANNEL
*Channel
691 VMBUS_CHANNEL_CLOSE_CHANNEL
* msg
;
692 VMBUS_CHANNEL_MSGINFO
* info
;
697 /* Stop callback and cancel the timer asap */
698 Channel
->OnChannelCallback
= NULL
;
699 TimerStop(Channel
->PollTimer
);
701 /* Send a closing message */
702 info
= kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL
), GFP_KERNEL
);
703 ASSERT(info
!= NULL
);
705 /* info->waitEvent = WaitEventCreate(); */
707 msg
= (VMBUS_CHANNEL_CLOSE_CHANNEL
*)info
->Msg
;
708 msg
->Header
.MessageType
= ChannelMessageCloseChannel
;
709 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
711 ret
= VmbusPostMessage(msg
, sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL
));
717 /* Tear down the gpadl for the channel's ring buffer */
718 if (Channel
->RingBufferGpadlHandle
)
720 VmbusChannelTeardownGpadl(Channel
, Channel
->RingBufferGpadlHandle
);
723 /* TODO: Send a msg to release the childRelId */
725 /* Cleanup the ring buffers for this channel */
726 RingBufferCleanup(&Channel
->Outbound
);
727 RingBufferCleanup(&Channel
->Inbound
);
729 PageFree(Channel
->RingBufferPages
, Channel
->RingBufferPageCount
);
735 * If we are closing the channel during an error path in
736 * opening the channel, don't free the channel since the
737 * caller will free the channel
740 if (Channel
->State
== CHANNEL_OPEN_STATE
)
742 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
743 REMOVE_ENTRY_LIST(&Channel
->ListEntry
);
744 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
746 FreeVmbusChannel(Channel
);
756 VmbusChannelSendPacket()
759 Send the specified buffer on the given channel
763 VmbusChannelSendPacket(
764 VMBUS_CHANNEL
*Channel
,
768 VMBUS_PACKET_TYPE Type
,
773 VMPACKET_DESCRIPTOR desc
;
774 u32 packetLen
= sizeof(VMPACKET_DESCRIPTOR
) + BufferLen
;
775 u32 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
776 SG_BUFFER_LIST bufferList
[3];
780 DPRINT_DBG(VMBUS
, "channel %p buffer %p len %d", Channel
, Buffer
, BufferLen
);
782 DumpVmbusChannel(Channel
);
784 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
786 /* Setup the descriptor */
787 desc
.Type
= Type
; /* VmbusPacketTypeDataInBand; */
788 desc
.Flags
= Flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
789 desc
.DataOffset8
= sizeof(VMPACKET_DESCRIPTOR
) >> 3; /* in 8-bytes granularity */
790 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
791 desc
.TransactionId
= RequestId
;
793 bufferList
[0].Data
= &desc
;
794 bufferList
[0].Length
= sizeof(VMPACKET_DESCRIPTOR
);
796 bufferList
[1].Data
= Buffer
;
797 bufferList
[1].Length
= BufferLen
;
799 bufferList
[2].Data
= &alignedData
;
800 bufferList
[2].Length
= packetLenAligned
- packetLen
;
802 ret
= RingBufferWrite(
807 /* TODO: We should determine if this is optional */
808 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
810 VmbusChannelSetEvent(Channel
);
822 VmbusChannelSendPacketPageBuffer()
825 Send a range of single-page buffer packets using a GPADL Direct packet type.
829 VmbusChannelSendPacketPageBuffer(
830 VMBUS_CHANNEL
*Channel
,
831 PAGE_BUFFER PageBuffers
[],
840 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc
;
843 u32 packetLenAligned
;
844 SG_BUFFER_LIST bufferList
[3];
849 ASSERT(PageCount
<= MAX_PAGE_BUFFER_COUNT
);
851 DumpVmbusChannel(Channel
);
853 /* Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the largest size we support */
854 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER
) - ((MAX_PAGE_BUFFER_COUNT
- PageCount
)*sizeof(PAGE_BUFFER
));
855 packetLen
= descSize
+ BufferLen
;
856 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
858 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
860 /* Setup the descriptor */
861 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
862 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
863 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
864 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
865 desc
.TransactionId
= RequestId
;
866 desc
.RangeCount
= PageCount
;
868 for (i
=0; i
<PageCount
; i
++)
870 desc
.Range
[i
].Length
= PageBuffers
[i
].Length
;
871 desc
.Range
[i
].Offset
= PageBuffers
[i
].Offset
;
872 desc
.Range
[i
].Pfn
= PageBuffers
[i
].Pfn
;
875 bufferList
[0].Data
= &desc
;
876 bufferList
[0].Length
= descSize
;
878 bufferList
[1].Data
= Buffer
;
879 bufferList
[1].Length
= BufferLen
;
881 bufferList
[2].Data
= &alignedData
;
882 bufferList
[2].Length
= packetLenAligned
- packetLen
;
884 ret
= RingBufferWrite(
889 /* TODO: We should determine if this is optional */
890 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
892 VmbusChannelSetEvent(Channel
);
905 VmbusChannelSendPacketMultiPageBuffer()
908 Send a multi-page buffer packet using a GPADL Direct packet type.
912 VmbusChannelSendPacketMultiPageBuffer(
913 VMBUS_CHANNEL
*Channel
,
914 MULTIPAGE_BUFFER
*MultiPageBuffer
,
921 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc
;
924 u32 packetLenAligned
;
925 SG_BUFFER_LIST bufferList
[3];
927 u32 PfnCount
= NUM_PAGES_SPANNED(MultiPageBuffer
->Offset
, MultiPageBuffer
->Length
);
931 DumpVmbusChannel(Channel
);
933 DPRINT_DBG(VMBUS
, "data buffer - offset %u len %u pfn count %u", MultiPageBuffer
->Offset
, MultiPageBuffer
->Length
, PfnCount
);
935 ASSERT(PfnCount
> 0);
936 ASSERT(PfnCount
<= MAX_MULTIPAGE_BUFFER_COUNT
);
938 /* Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is the largest size we support */
939 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER
) - ((MAX_MULTIPAGE_BUFFER_COUNT
- PfnCount
)*sizeof(u64
));
940 packetLen
= descSize
+ BufferLen
;
941 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
943 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
945 /* Setup the descriptor */
946 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
947 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
948 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
949 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
950 desc
.TransactionId
= RequestId
;
953 desc
.Range
.Length
= MultiPageBuffer
->Length
;
954 desc
.Range
.Offset
= MultiPageBuffer
->Offset
;
956 memcpy(desc
.Range
.PfnArray
, MultiPageBuffer
->PfnArray
, PfnCount
*sizeof(u64
));
958 bufferList
[0].Data
= &desc
;
959 bufferList
[0].Length
= descSize
;
961 bufferList
[1].Data
= Buffer
;
962 bufferList
[1].Length
= BufferLen
;
964 bufferList
[2].Data
= &alignedData
;
965 bufferList
[2].Length
= packetLenAligned
- packetLen
;
967 ret
= RingBufferWrite(
972 /* TODO: We should determine if this is optional */
973 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
975 VmbusChannelSetEvent(Channel
);
987 VmbusChannelRecvPacket()
990 Retrieve the user packet on the specified channel
993 /* TODO: Do we ever receive a gpa direct packet other than the ones we send ? */
995 VmbusChannelRecvPacket(
996 VMBUS_CHANNEL
*Channel
,
999 u32
* BufferActualLen
,
1003 VMPACKET_DESCRIPTOR desc
;
1007 unsigned long flags
;
1009 DPRINT_ENTER(VMBUS
);
1011 *BufferActualLen
= 0;
1014 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
1016 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
, sizeof(VMPACKET_DESCRIPTOR
));
1019 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1021 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1026 /* VmbusChannelClearEvent(Channel); */
1028 packetLen
= desc
.Length8
<< 3;
1029 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
1030 /* ASSERT(userLen > 0); */
1032 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1034 Channel
->OfferMsg
.ChildRelId
,
1037 desc
.TransactionId
, packetLen
, userLen
);
1039 *BufferActualLen
= userLen
;
1041 if (userLen
> BufferLen
)
1043 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1045 DPRINT_ERR(VMBUS
, "buffer too small - got %d needs %d", BufferLen
, userLen
);
1051 *RequestId
= desc
.TransactionId
;
1053 /* Copy over the packet to the user buffer */
1054 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, userLen
, (desc
.DataOffset8
<< 3));
1056 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1066 VmbusChannelRecvPacketRaw()
1069 Retrieve the raw packet on the specified channel
1073 VmbusChannelRecvPacketRaw(
1074 VMBUS_CHANNEL
*Channel
,
1077 u32
* BufferActualLen
,
1081 VMPACKET_DESCRIPTOR desc
;
1085 unsigned long flags
;
1087 DPRINT_ENTER(VMBUS
);
1089 *BufferActualLen
= 0;
1092 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
1094 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
, sizeof(VMPACKET_DESCRIPTOR
));
1097 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1099 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1104 /* VmbusChannelClearEvent(Channel); */
1106 packetLen
= desc
.Length8
<< 3;
1107 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
1109 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1111 Channel
->OfferMsg
.ChildRelId
,
1114 desc
.TransactionId
, packetLen
, userLen
);
1116 *BufferActualLen
= packetLen
;
1118 if (packetLen
> BufferLen
)
1120 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1122 DPRINT_ERR(VMBUS
, "buffer too small - needed %d bytes but got space for only %d bytes", packetLen
, BufferLen
);
1127 *RequestId
= desc
.TransactionId
;
1129 /* Copy over the entire packet to the user buffer */
1130 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, packetLen
, 0);
1132 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1143 VmbusChannelOnChannelEvent()
1146 Channel event callback
1150 VmbusChannelOnChannelEvent(
1151 VMBUS_CHANNEL
*Channel
1154 DumpVmbusChannel(Channel
);
1155 ASSERT(Channel
->OnChannelCallback
);
1156 #ifdef ENABLE_POLLING
1157 TimerStop(Channel
->PollTimer
);
1158 Channel
->OnChannelCallback(Channel
->ChannelCallbackContext
);
1159 TimerStart(Channel
->PollTimer
, 100 /* 100us */);
1161 Channel
->OnChannelCallback(Channel
->ChannelCallbackContext
);
1168 VmbusChannelOnTimer()
1171 Timer event callback
1175 VmbusChannelOnTimer(
1179 VMBUS_CHANNEL
*channel
= (VMBUS_CHANNEL
*)Context
;
1181 if (channel
->OnChannelCallback
)
1183 channel
->OnChannelCallback(channel
->ChannelCallbackContext
);
1184 #ifdef ENABLE_POLLING
1185 TimerStart(channel
->PollTimer
, 100 /* 100us */);
1197 Dump vmbus channel info to the console
1202 VMBUS_CHANNEL
*Channel
1205 DPRINT_DBG(VMBUS
, "Channel (%d)", Channel
->OfferMsg
.ChildRelId
);
1206 DumpRingInfo(&Channel
->Outbound
, "Outbound ");
1207 DumpRingInfo(&Channel
->Inbound
, "Inbound ");