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 BitSet((u32
*)gVmbusConnection
.SendInterruptPage
+ (Channel
->OfferMsg
.ChildRelId
>> 5), Channel
->OfferMsg
.ChildRelId
& 31);
109 monitorPage
= (HV_MONITOR_PAGE
*)gVmbusConnection
.MonitorPages
;
110 monitorPage
++; /* Get the child to parent monitor page */
112 BitSet((u32
*) &monitorPage
->TriggerGroup
[Channel
->MonitorGroup
].Pending
, Channel
->MonitorBit
);
116 VmbusSetEvent(Channel
->OfferMsg
.ChildRelId
);
124 VmbusChannelClearEvent(
125 VMBUS_CHANNEL
*Channel
128 HV_MONITOR_PAGE
*monitorPage
;
132 if (Channel
->OfferMsg
.MonitorAllocated
)
134 /* Each u32 represents 32 channels */
135 BitClear((u32
*)gVmbusConnection
.SendInterruptPage
+ (Channel
->OfferMsg
.ChildRelId
>> 5), Channel
->OfferMsg
.ChildRelId
& 31);
137 monitorPage
= (HV_MONITOR_PAGE
*)gVmbusConnection
.MonitorPages
;
138 monitorPage
++; /* Get the child to parent monitor page */
140 BitClear((u32
*) &monitorPage
->TriggerGroup
[Channel
->MonitorGroup
].Pending
, Channel
->MonitorBit
);
150 VmbusChannelGetDebugInfo()
153 Retrieve various channel debug info
157 VmbusChannelGetDebugInfo(
158 VMBUS_CHANNEL
*Channel
,
159 VMBUS_CHANNEL_DEBUG_INFO
*DebugInfo
162 HV_MONITOR_PAGE
*monitorPage
;
163 u8 monitorGroup
= (u8
)Channel
->OfferMsg
.MonitorId
/ 32;
164 u8 monitorOffset
= (u8
)Channel
->OfferMsg
.MonitorId
% 32;
165 /* u32 monitorBit = 1 << monitorOffset; */
167 DebugInfo
->RelId
= Channel
->OfferMsg
.ChildRelId
;
168 DebugInfo
->State
= Channel
->State
;
169 memcpy(&DebugInfo
->InterfaceType
, &Channel
->OfferMsg
.Offer
.InterfaceType
, sizeof(GUID
));
170 memcpy(&DebugInfo
->InterfaceInstance
, &Channel
->OfferMsg
.Offer
.InterfaceInstance
, sizeof(GUID
));
172 monitorPage
= (HV_MONITOR_PAGE
*)gVmbusConnection
.MonitorPages
;
174 DebugInfo
->MonitorId
= Channel
->OfferMsg
.MonitorId
;
176 DebugInfo
->ServerMonitorPending
= monitorPage
->TriggerGroup
[monitorGroup
].Pending
;
177 DebugInfo
->ServerMonitorLatency
= monitorPage
->Latency
[monitorGroup
][ monitorOffset
];
178 DebugInfo
->ServerMonitorConnectionId
= monitorPage
->Parameter
[monitorGroup
][ monitorOffset
].ConnectionId
.u
.Id
;
182 DebugInfo
->ClientMonitorPending
= monitorPage
->TriggerGroup
[monitorGroup
].Pending
;
183 DebugInfo
->ClientMonitorLatency
= monitorPage
->Latency
[monitorGroup
][ monitorOffset
];
184 DebugInfo
->ClientMonitorConnectionId
= monitorPage
->Parameter
[monitorGroup
][ monitorOffset
].ConnectionId
.u
.Id
;
186 RingBufferGetDebugInfo(&Channel
->Inbound
, &DebugInfo
->Inbound
);
187 RingBufferGetDebugInfo(&Channel
->Outbound
, &DebugInfo
->Outbound
);
197 Open the specified channel.
202 VMBUS_CHANNEL
*NewChannel
,
203 u32 SendRingBufferSize
,
204 u32 RecvRingBufferSize
,
207 PFN_CHANNEL_CALLBACK pfnOnChannelCallback
,
212 VMBUS_CHANNEL_OPEN_CHANNEL
* openMsg
;
213 VMBUS_CHANNEL_MSGINFO
* openInfo
;
219 /* Aligned to page size */
220 ASSERT(!(SendRingBufferSize
& (PAGE_SIZE
-1)));
221 ASSERT(!(RecvRingBufferSize
& (PAGE_SIZE
-1)));
223 NewChannel
->OnChannelCallback
= pfnOnChannelCallback
;
224 NewChannel
->ChannelCallbackContext
= Context
;
226 /* Allocate the ring buffer */
227 out
= PageAlloc((SendRingBufferSize
+ RecvRingBufferSize
) >> PAGE_SHIFT
);
228 /* out = kzalloc(sendRingBufferSize + recvRingBufferSize, GFP_KERNEL); */
230 ASSERT(((unsigned long)out
& (PAGE_SIZE
-1)) == 0);
232 in
= (void*)((unsigned long)out
+ SendRingBufferSize
);
234 NewChannel
->RingBufferPages
= out
;
235 NewChannel
->RingBufferPageCount
= (SendRingBufferSize
+ RecvRingBufferSize
) >> PAGE_SHIFT
;
237 RingBufferInit(&NewChannel
->Outbound
, out
, SendRingBufferSize
);
239 RingBufferInit(&NewChannel
->Inbound
, in
, RecvRingBufferSize
);
241 /* Establish the gpadl for the ring buffer */
242 DPRINT_DBG(VMBUS
, "Establishing ring buffer's gpadl for channel %p...", NewChannel
);
244 NewChannel
->RingBufferGpadlHandle
= 0;
246 ret
= VmbusChannelEstablishGpadl(NewChannel
,
247 NewChannel
->Outbound
.RingBuffer
,
248 SendRingBufferSize
+ RecvRingBufferSize
,
249 &NewChannel
->RingBufferGpadlHandle
);
251 DPRINT_DBG(VMBUS
, "channel %p <relid %d gpadl 0x%x send ring %p size %d recv ring %p size %d, downstreamoffset %d>",
253 NewChannel
->OfferMsg
.ChildRelId
,
254 NewChannel
->RingBufferGpadlHandle
,
255 NewChannel
->Outbound
.RingBuffer
,
256 NewChannel
->Outbound
.RingSize
,
257 NewChannel
->Inbound
.RingBuffer
,
258 NewChannel
->Inbound
.RingSize
,
261 /* Create and init the channel open message */
262 openInfo
= kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_OPEN_CHANNEL
), GFP_KERNEL
);
263 ASSERT(openInfo
!= NULL
);
265 openInfo
->WaitEvent
= WaitEventCreate();
267 openMsg
= (VMBUS_CHANNEL_OPEN_CHANNEL
*)openInfo
->Msg
;
268 openMsg
->Header
.MessageType
= ChannelMessageOpenChannel
;
269 openMsg
->OpenId
= NewChannel
->OfferMsg
.ChildRelId
; /* FIXME */
270 openMsg
->ChildRelId
= NewChannel
->OfferMsg
.ChildRelId
;
271 openMsg
->RingBufferGpadlHandle
= NewChannel
->RingBufferGpadlHandle
;
272 ASSERT(openMsg
->RingBufferGpadlHandle
);
273 openMsg
->DownstreamRingBufferPageOffset
= SendRingBufferSize
>> PAGE_SHIFT
;
274 openMsg
->ServerContextAreaGpadlHandle
= 0; /* TODO */
276 ASSERT(UserDataLen
<= MAX_USER_DEFINED_BYTES
);
279 memcpy(openMsg
->UserData
, UserData
, UserDataLen
);
282 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
283 INSERT_TAIL_LIST(&gVmbusConnection
.ChannelMsgList
, &openInfo
->MsgListEntry
);
284 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
286 DPRINT_DBG(VMBUS
, "Sending channel open msg...");
288 ret
= VmbusPostMessage(openMsg
, sizeof(VMBUS_CHANNEL_OPEN_CHANNEL
));
291 DPRINT_ERR(VMBUS
, "unable to open channel - %d", ret
);
295 /* FIXME: Need to time-out here */
296 WaitEventWait(openInfo
->WaitEvent
);
298 if (openInfo
->Response
.OpenResult
.Status
== 0)
300 DPRINT_INFO(VMBUS
, "channel <%p> open success!!", NewChannel
);
304 DPRINT_INFO(VMBUS
, "channel <%p> open failed - %d!!", NewChannel
, openInfo
->Response
.OpenResult
.Status
);
308 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
309 REMOVE_ENTRY_LIST(&openInfo
->MsgListEntry
);
310 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
312 kfree(openInfo
->WaitEvent
);
326 Dump the gpadl body message to the console for debugging purposes.
329 static void DumpGpadlBody(
330 VMBUS_CHANNEL_GPADL_BODY
*Gpadl
,
336 pfnCount
= (Len
- sizeof(VMBUS_CHANNEL_GPADL_BODY
))/ sizeof(u64
);
337 DPRINT_DBG(VMBUS
, "gpadl body - len %d pfn count %d", Len
, pfnCount
);
339 for (i
=0; i
< pfnCount
; i
++)
341 DPRINT_DBG(VMBUS
, "gpadl body - %d) pfn %llu", i
, Gpadl
->Pfn
[i
]);
352 Dump the gpadl header message to the console for debugging purposes.
355 static void DumpGpadlHeader(
356 VMBUS_CHANNEL_GPADL_HEADER
*Gpadl
363 DPRINT_DBG(VMBUS
, "gpadl header - relid %d, range count %d, range buflen %d",
367 for (i
=0; i
< Gpadl
->RangeCount
; i
++)
369 pageCount
= Gpadl
->Range
[i
].ByteCount
>> PAGE_SHIFT
;
370 pageCount
= (pageCount
> 26)? 26 : pageCount
;
372 DPRINT_DBG(VMBUS
, "gpadl range %d - len %d offset %d page count %d",
373 i
, Gpadl
->Range
[i
].ByteCount
, Gpadl
->Range
[i
].ByteOffset
, pageCount
);
375 for (j
=0; j
< pageCount
; j
++)
377 DPRINT_DBG(VMBUS
, "%d) pfn %llu", j
, Gpadl
->Range
[i
].PfnArray
[j
]);
385 VmbusChannelCreateGpadlHeader()
388 Creates a gpadl for the specified buffer
392 VmbusChannelCreateGpadlHeader(
393 void * Kbuffer
, /* from kmalloc() */
394 u32 Size
, /* page-size multiple */
395 VMBUS_CHANNEL_MSGINFO
**MsgInfo
,
400 unsigned long long pfn
;
401 VMBUS_CHANNEL_GPADL_HEADER
* gpaHeader
;
402 VMBUS_CHANNEL_GPADL_BODY
* gpadlBody
;
403 VMBUS_CHANNEL_MSGINFO
* msgHeader
;
404 VMBUS_CHANNEL_MSGINFO
* msgBody
;
407 int pfnSum
, pfnCount
, pfnLeft
, pfnCurr
, pfnSize
;
409 /* ASSERT( (kbuffer & (PAGE_SIZE-1)) == 0); */
410 ASSERT( (Size
& (PAGE_SIZE
-1)) == 0);
412 pageCount
= Size
>> PAGE_SHIFT
;
413 pfn
= GetPhysicalAddress(Kbuffer
) >> PAGE_SHIFT
;
415 /* do we need a gpadl body msg */
416 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
- sizeof(VMBUS_CHANNEL_GPADL_HEADER
) - sizeof(GPA_RANGE
);
417 pfnCount
= pfnSize
/ sizeof(u64
);
419 if (pageCount
> pfnCount
) /* we need a gpadl body */
421 /* fill in the header */
422 msgSize
= sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_GPADL_HEADER
) + sizeof(GPA_RANGE
) + pfnCount
*sizeof(u64
);
423 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
425 INITIALIZE_LIST_HEAD(&msgHeader
->SubMsgList
);
426 msgHeader
->MessageSize
=msgSize
;
428 gpaHeader
= (VMBUS_CHANNEL_GPADL_HEADER
*)msgHeader
->Msg
;
429 gpaHeader
->RangeCount
= 1;
430 gpaHeader
->RangeBufLen
= sizeof(GPA_RANGE
) + pageCount
*sizeof(u64
);
431 gpaHeader
->Range
[0].ByteOffset
= 0;
432 gpaHeader
->Range
[0].ByteCount
= Size
;
433 for (i
=0; i
<pfnCount
; i
++)
435 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
437 *MsgInfo
= msgHeader
;
441 pfnLeft
= pageCount
- pfnCount
;
443 /* how many pfns can we fit */
444 pfnSize
= MAX_SIZE_CHANNEL_MESSAGE
- sizeof(VMBUS_CHANNEL_GPADL_BODY
);
445 pfnCount
= pfnSize
/ sizeof(u64
);
447 /* fill in the body */
450 if (pfnLeft
> pfnCount
)
459 msgSize
= sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_GPADL_BODY
) + pfnCurr
*sizeof(u64
);
460 msgBody
= kzalloc(msgSize
, GFP_KERNEL
);
462 msgBody
->MessageSize
= msgSize
;
464 gpadlBody
= (VMBUS_CHANNEL_GPADL_BODY
*)msgBody
->Msg
;
466 /* FIXME: Gpadl is u32 and we are using a pointer which could be 64-bit */
467 /* gpadlBody->Gpadl = kbuffer; */
468 for (i
=0; i
<pfnCurr
; i
++)
470 gpadlBody
->Pfn
[i
] = pfn
+ pfnSum
+ i
;
473 /* add to msg header */
474 INSERT_TAIL_LIST(&msgHeader
->SubMsgList
, &msgBody
->MsgListEntry
);
481 /* everything fits in a header */
482 msgSize
= sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_GPADL_HEADER
) + sizeof(GPA_RANGE
) + pageCount
*sizeof(u64
);
483 msgHeader
= kzalloc(msgSize
, GFP_KERNEL
);
484 msgHeader
->MessageSize
=msgSize
;
486 gpaHeader
= (VMBUS_CHANNEL_GPADL_HEADER
*)msgHeader
->Msg
;
487 gpaHeader
->RangeCount
= 1;
488 gpaHeader
->RangeBufLen
= sizeof(GPA_RANGE
) + pageCount
*sizeof(u64
);
489 gpaHeader
->Range
[0].ByteOffset
= 0;
490 gpaHeader
->Range
[0].ByteCount
= Size
;
491 for (i
=0; i
<pageCount
; i
++)
493 gpaHeader
->Range
[0].PfnArray
[i
] = pfn
+i
;
496 *MsgInfo
= msgHeader
;
507 VmbusChannelEstablishGpadl()
510 Estabish a GPADL for the specified buffer
514 VmbusChannelEstablishGpadl(
515 VMBUS_CHANNEL
*Channel
,
516 void * Kbuffer
, /* from kmalloc() */
517 u32 Size
, /* page-size multiple */
522 VMBUS_CHANNEL_GPADL_HEADER
* gpadlMsg
;
523 VMBUS_CHANNEL_GPADL_BODY
* gpadlBody
;
524 /* VMBUS_CHANNEL_GPADL_CREATED* gpadlCreated; */
526 VMBUS_CHANNEL_MSGINFO
*msgInfo
;
527 VMBUS_CHANNEL_MSGINFO
*subMsgInfo
;
537 nextGpadlHandle
= gVmbusConnection
.NextGpadlHandle
;
538 InterlockedIncrement((int*)&gVmbusConnection
.NextGpadlHandle
);
540 VmbusChannelCreateGpadlHeader(Kbuffer
, Size
, &msgInfo
, &msgCount
);
541 ASSERT(msgInfo
!= NULL
);
544 msgInfo
->WaitEvent
= WaitEventCreate();
545 gpadlMsg
= (VMBUS_CHANNEL_GPADL_HEADER
*)msgInfo
->Msg
;
546 gpadlMsg
->Header
.MessageType
= ChannelMessageGpadlHeader
;
547 gpadlMsg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
548 gpadlMsg
->Gpadl
= nextGpadlHandle
;
550 DumpGpadlHeader(gpadlMsg
);
552 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
553 INSERT_TAIL_LIST(&gVmbusConnection
.ChannelMsgList
, &msgInfo
->MsgListEntry
);
554 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
556 DPRINT_DBG(VMBUS
, "buffer %p, size %d msg cnt %d", Kbuffer
, Size
, msgCount
);
558 DPRINT_DBG(VMBUS
, "Sending GPADL Header - len %zd", msgInfo
->MessageSize
- sizeof(VMBUS_CHANNEL_MSGINFO
));
560 ret
= VmbusPostMessage(gpadlMsg
, msgInfo
->MessageSize
- sizeof(VMBUS_CHANNEL_MSGINFO
));
563 DPRINT_ERR(VMBUS
, "Unable to open channel - %d", ret
);
569 ITERATE_LIST_ENTRIES(anchor
, curr
, &msgInfo
->SubMsgList
)
571 subMsgInfo
= (VMBUS_CHANNEL_MSGINFO
*) curr
;
572 gpadlBody
= (VMBUS_CHANNEL_GPADL_BODY
*)subMsgInfo
->Msg
;
574 gpadlBody
->Header
.MessageType
= ChannelMessageGpadlBody
;
575 gpadlBody
->Gpadl
= nextGpadlHandle
;
577 DPRINT_DBG(VMBUS
, "Sending GPADL Body - len %zd", subMsgInfo
->MessageSize
- sizeof(VMBUS_CHANNEL_MSGINFO
));
579 DumpGpadlBody(gpadlBody
, subMsgInfo
->MessageSize
- sizeof(VMBUS_CHANNEL_MSGINFO
));
580 ret
= VmbusPostMessage(gpadlBody
, subMsgInfo
->MessageSize
- sizeof(VMBUS_CHANNEL_MSGINFO
));
584 WaitEventWait(msgInfo
->WaitEvent
);
586 /* At this point, we received the gpadl created msg */
587 DPRINT_DBG(VMBUS
, "Received GPADL created (relid %d, status %d handle %x)",
588 Channel
->OfferMsg
.ChildRelId
,
589 msgInfo
->Response
.GpadlCreated
.CreationStatus
,
592 *GpadlHandle
= gpadlMsg
->Gpadl
;
595 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
596 REMOVE_ENTRY_LIST(&msgInfo
->MsgListEntry
);
597 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
599 kfree(msgInfo
->WaitEvent
);
612 VmbusChannelTeardownGpadl()
615 Teardown the specified GPADL handle
619 VmbusChannelTeardownGpadl(
620 VMBUS_CHANNEL
*Channel
,
625 VMBUS_CHANNEL_GPADL_TEARDOWN
*msg
;
626 VMBUS_CHANNEL_MSGINFO
* info
;
631 ASSERT(GpadlHandle
!= 0);
633 info
= kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN
), GFP_KERNEL
);
634 ASSERT(info
!= NULL
);
636 info
->WaitEvent
= WaitEventCreate();
638 msg
= (VMBUS_CHANNEL_GPADL_TEARDOWN
*)info
->Msg
;
640 msg
->Header
.MessageType
= ChannelMessageGpadlTeardown
;
641 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
642 msg
->Gpadl
= GpadlHandle
;
644 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
645 INSERT_TAIL_LIST(&gVmbusConnection
.ChannelMsgList
, &info
->MsgListEntry
);
646 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
648 ret
= VmbusPostMessage(msg
, sizeof(VMBUS_CHANNEL_GPADL_TEARDOWN
));
654 WaitEventWait(info
->WaitEvent
);
656 /* Received a torndown response */
657 spin_lock_irqsave(&gVmbusConnection
.channelmsg_lock
, flags
);
658 REMOVE_ENTRY_LIST(&info
->MsgListEntry
);
659 spin_unlock_irqrestore(&gVmbusConnection
.channelmsg_lock
, flags
);
661 kfree(info
->WaitEvent
);
676 Close the specified channel
681 VMBUS_CHANNEL
*Channel
685 VMBUS_CHANNEL_CLOSE_CHANNEL
* msg
;
686 VMBUS_CHANNEL_MSGINFO
* info
;
691 /* Stop callback and cancel the timer asap */
692 Channel
->OnChannelCallback
= NULL
;
693 TimerStop(Channel
->PollTimer
);
695 /* Send a closing message */
696 info
= kmalloc(sizeof(VMBUS_CHANNEL_MSGINFO
) + sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL
), GFP_KERNEL
);
697 ASSERT(info
!= NULL
);
699 /* info->waitEvent = WaitEventCreate(); */
701 msg
= (VMBUS_CHANNEL_CLOSE_CHANNEL
*)info
->Msg
;
702 msg
->Header
.MessageType
= ChannelMessageCloseChannel
;
703 msg
->ChildRelId
= Channel
->OfferMsg
.ChildRelId
;
705 ret
= VmbusPostMessage(msg
, sizeof(VMBUS_CHANNEL_CLOSE_CHANNEL
));
711 /* Tear down the gpadl for the channel's ring buffer */
712 if (Channel
->RingBufferGpadlHandle
)
714 VmbusChannelTeardownGpadl(Channel
, Channel
->RingBufferGpadlHandle
);
717 /* TODO: Send a msg to release the childRelId */
719 /* Cleanup the ring buffers for this channel */
720 RingBufferCleanup(&Channel
->Outbound
);
721 RingBufferCleanup(&Channel
->Inbound
);
723 PageFree(Channel
->RingBufferPages
, Channel
->RingBufferPageCount
);
729 * If we are closing the channel during an error path in
730 * opening the channel, don't free the channel since the
731 * caller will free the channel
734 if (Channel
->State
== CHANNEL_OPEN_STATE
)
736 spin_lock_irqsave(&gVmbusConnection
.channel_lock
, flags
);
737 REMOVE_ENTRY_LIST(&Channel
->ListEntry
);
738 spin_unlock_irqrestore(&gVmbusConnection
.channel_lock
, flags
);
740 FreeVmbusChannel(Channel
);
750 VmbusChannelSendPacket()
753 Send the specified buffer on the given channel
757 VmbusChannelSendPacket(
758 VMBUS_CHANNEL
*Channel
,
762 VMBUS_PACKET_TYPE Type
,
767 VMPACKET_DESCRIPTOR desc
;
768 u32 packetLen
= sizeof(VMPACKET_DESCRIPTOR
) + BufferLen
;
769 u32 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
770 SG_BUFFER_LIST bufferList
[3];
774 DPRINT_DBG(VMBUS
, "channel %p buffer %p len %d", Channel
, Buffer
, BufferLen
);
776 DumpVmbusChannel(Channel
);
778 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
780 /* Setup the descriptor */
781 desc
.Type
= Type
; /* VmbusPacketTypeDataInBand; */
782 desc
.Flags
= Flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
783 desc
.DataOffset8
= sizeof(VMPACKET_DESCRIPTOR
) >> 3; /* in 8-bytes granularity */
784 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
785 desc
.TransactionId
= RequestId
;
787 bufferList
[0].Data
= &desc
;
788 bufferList
[0].Length
= sizeof(VMPACKET_DESCRIPTOR
);
790 bufferList
[1].Data
= Buffer
;
791 bufferList
[1].Length
= BufferLen
;
793 bufferList
[2].Data
= &alignedData
;
794 bufferList
[2].Length
= packetLenAligned
- packetLen
;
796 ret
= RingBufferWrite(
801 /* TODO: We should determine if this is optional */
802 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
804 VmbusChannelSetEvent(Channel
);
816 VmbusChannelSendPacketPageBuffer()
819 Send a range of single-page buffer packets using a GPADL Direct packet type.
823 VmbusChannelSendPacketPageBuffer(
824 VMBUS_CHANNEL
*Channel
,
825 PAGE_BUFFER PageBuffers
[],
834 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc
;
837 u32 packetLenAligned
;
838 SG_BUFFER_LIST bufferList
[3];
843 ASSERT(PageCount
<= MAX_PAGE_BUFFER_COUNT
);
845 DumpVmbusChannel(Channel
);
847 /* Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the largest size we support */
848 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER
) - ((MAX_PAGE_BUFFER_COUNT
- PageCount
)*sizeof(PAGE_BUFFER
));
849 packetLen
= descSize
+ BufferLen
;
850 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
852 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
854 /* Setup the descriptor */
855 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
856 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
857 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
858 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
859 desc
.TransactionId
= RequestId
;
860 desc
.RangeCount
= PageCount
;
862 for (i
=0; i
<PageCount
; i
++)
864 desc
.Range
[i
].Length
= PageBuffers
[i
].Length
;
865 desc
.Range
[i
].Offset
= PageBuffers
[i
].Offset
;
866 desc
.Range
[i
].Pfn
= PageBuffers
[i
].Pfn
;
869 bufferList
[0].Data
= &desc
;
870 bufferList
[0].Length
= descSize
;
872 bufferList
[1].Data
= Buffer
;
873 bufferList
[1].Length
= BufferLen
;
875 bufferList
[2].Data
= &alignedData
;
876 bufferList
[2].Length
= packetLenAligned
- packetLen
;
878 ret
= RingBufferWrite(
883 /* TODO: We should determine if this is optional */
884 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
886 VmbusChannelSetEvent(Channel
);
899 VmbusChannelSendPacketMultiPageBuffer()
902 Send a multi-page buffer packet using a GPADL Direct packet type.
906 VmbusChannelSendPacketMultiPageBuffer(
907 VMBUS_CHANNEL
*Channel
,
908 MULTIPAGE_BUFFER
*MultiPageBuffer
,
915 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc
;
918 u32 packetLenAligned
;
919 SG_BUFFER_LIST bufferList
[3];
921 u32 PfnCount
= NUM_PAGES_SPANNED(MultiPageBuffer
->Offset
, MultiPageBuffer
->Length
);
925 DumpVmbusChannel(Channel
);
927 DPRINT_DBG(VMBUS
, "data buffer - offset %u len %u pfn count %u", MultiPageBuffer
->Offset
, MultiPageBuffer
->Length
, PfnCount
);
929 ASSERT(PfnCount
> 0);
930 ASSERT(PfnCount
<= MAX_MULTIPAGE_BUFFER_COUNT
);
932 /* Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is the largest size we support */
933 descSize
= sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER
) - ((MAX_MULTIPAGE_BUFFER_COUNT
- PfnCount
)*sizeof(u64
));
934 packetLen
= descSize
+ BufferLen
;
935 packetLenAligned
= ALIGN_UP(packetLen
, sizeof(u64
));
937 ASSERT((packetLenAligned
- packetLen
) < sizeof(u64
));
939 /* Setup the descriptor */
940 desc
.Type
= VmbusPacketTypeDataUsingGpaDirect
;
941 desc
.Flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
942 desc
.DataOffset8
= descSize
>> 3; /* in 8-bytes grandularity */
943 desc
.Length8
= (u16
)(packetLenAligned
>> 3);
944 desc
.TransactionId
= RequestId
;
947 desc
.Range
.Length
= MultiPageBuffer
->Length
;
948 desc
.Range
.Offset
= MultiPageBuffer
->Offset
;
950 memcpy(desc
.Range
.PfnArray
, MultiPageBuffer
->PfnArray
, PfnCount
*sizeof(u64
));
952 bufferList
[0].Data
= &desc
;
953 bufferList
[0].Length
= descSize
;
955 bufferList
[1].Data
= Buffer
;
956 bufferList
[1].Length
= BufferLen
;
958 bufferList
[2].Data
= &alignedData
;
959 bufferList
[2].Length
= packetLenAligned
- packetLen
;
961 ret
= RingBufferWrite(
966 /* TODO: We should determine if this is optional */
967 if (ret
== 0 && !GetRingBufferInterruptMask(&Channel
->Outbound
))
969 VmbusChannelSetEvent(Channel
);
981 VmbusChannelRecvPacket()
984 Retrieve the user packet on the specified channel
987 /* TODO: Do we ever receive a gpa direct packet other than the ones we send ? */
989 VmbusChannelRecvPacket(
990 VMBUS_CHANNEL
*Channel
,
993 u32
* BufferActualLen
,
997 VMPACKET_DESCRIPTOR desc
;
1001 unsigned long flags
;
1003 DPRINT_ENTER(VMBUS
);
1005 *BufferActualLen
= 0;
1008 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
1010 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
, sizeof(VMPACKET_DESCRIPTOR
));
1013 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1015 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1020 /* VmbusChannelClearEvent(Channel); */
1022 packetLen
= desc
.Length8
<< 3;
1023 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
1024 /* ASSERT(userLen > 0); */
1026 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1028 Channel
->OfferMsg
.ChildRelId
,
1031 desc
.TransactionId
, packetLen
, userLen
);
1033 *BufferActualLen
= userLen
;
1035 if (userLen
> BufferLen
)
1037 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1039 DPRINT_ERR(VMBUS
, "buffer too small - got %d needs %d", BufferLen
, userLen
);
1045 *RequestId
= desc
.TransactionId
;
1047 /* Copy over the packet to the user buffer */
1048 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, userLen
, (desc
.DataOffset8
<< 3));
1050 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1060 VmbusChannelRecvPacketRaw()
1063 Retrieve the raw packet on the specified channel
1067 VmbusChannelRecvPacketRaw(
1068 VMBUS_CHANNEL
*Channel
,
1071 u32
* BufferActualLen
,
1075 VMPACKET_DESCRIPTOR desc
;
1079 unsigned long flags
;
1081 DPRINT_ENTER(VMBUS
);
1083 *BufferActualLen
= 0;
1086 spin_lock_irqsave(&Channel
->inbound_lock
, flags
);
1088 ret
= RingBufferPeek(&Channel
->Inbound
, &desc
, sizeof(VMPACKET_DESCRIPTOR
));
1091 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1093 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1098 /* VmbusChannelClearEvent(Channel); */
1100 packetLen
= desc
.Length8
<< 3;
1101 userLen
= packetLen
- (desc
.DataOffset8
<< 3);
1103 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1105 Channel
->OfferMsg
.ChildRelId
,
1108 desc
.TransactionId
, packetLen
, userLen
);
1110 *BufferActualLen
= packetLen
;
1112 if (packetLen
> BufferLen
)
1114 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1116 DPRINT_ERR(VMBUS
, "buffer too small - needed %d bytes but got space for only %d bytes", packetLen
, BufferLen
);
1121 *RequestId
= desc
.TransactionId
;
1123 /* Copy over the entire packet to the user buffer */
1124 ret
= RingBufferRead(&Channel
->Inbound
, Buffer
, packetLen
, 0);
1126 spin_unlock_irqrestore(&Channel
->inbound_lock
, flags
);
1137 VmbusChannelOnChannelEvent()
1140 Channel event callback
1144 VmbusChannelOnChannelEvent(
1145 VMBUS_CHANNEL
*Channel
1148 DumpVmbusChannel(Channel
);
1149 ASSERT(Channel
->OnChannelCallback
);
1150 #ifdef ENABLE_POLLING
1151 TimerStop(Channel
->PollTimer
);
1152 Channel
->OnChannelCallback(Channel
->ChannelCallbackContext
);
1153 TimerStart(Channel
->PollTimer
, 100 /* 100us */);
1155 Channel
->OnChannelCallback(Channel
->ChannelCallbackContext
);
1162 VmbusChannelOnTimer()
1165 Timer event callback
1169 VmbusChannelOnTimer(
1173 VMBUS_CHANNEL
*channel
= (VMBUS_CHANNEL
*)Context
;
1175 if (channel
->OnChannelCallback
)
1177 channel
->OnChannelCallback(channel
->ChannelCallbackContext
);
1178 #ifdef ENABLE_POLLING
1179 TimerStart(channel
->PollTimer
, 100 /* 100us */);
1191 Dump vmbus channel info to the console
1196 VMBUS_CHANNEL
*Channel
1199 DPRINT_DBG(VMBUS
, "Channel (%d)", Channel
->OfferMsg
.ChildRelId
);
1200 DumpRingInfo(&Channel
->Outbound
, "Outbound ");
1201 DumpRingInfo(&Channel
->Inbound
, "Inbound ");