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>
27 #include "include/logging.h"
29 #include "VmbusPrivate.h"
31 /* Internal routines */
32 static int VmbusChannelCreateGpadlHeader(
33 void * Kbuffer, /* must be phys and virt contiguous */
34 u32 Size, /* page-size multiple */
35 struct vmbus_channel_msginfo **msgInfo,
38 static void DumpVmbusChannel(struct vmbus_channel *channel);
39 static void VmbusChannelSetEvent(struct vmbus_channel *channel);
45 struct hv_monitor_page *MonitorPage
51 DPRINT_DBG(VMBUS, "monitorPage - %p, trigger state - %d", MonitorPage, MonitorPage->TriggerState);
55 DPRINT_DBG(VMBUS, "trigger group (%d) - %llx", i, MonitorPage->TriggerGroup[i].AsUINT64);
62 DPRINT_DBG(VMBUS, "latency (%d)(%d) - %llx", i, j, MonitorPage->Latency[i][j]);
69 DPRINT_DBG(VMBUS, "param-conn id (%d)(%d) - %d", i, j, MonitorPage->Parameter[i][j].ConnectionId.Asu32);
70 DPRINT_DBG(VMBUS, "param-flag (%d)(%d) - %d", i, j, MonitorPage->Parameter[i][j].FlagNumber);
80 VmbusChannelSetEvent()
83 Trigger an event notification on the specified channel.
86 static void VmbusChannelSetEvent(struct vmbus_channel *Channel)
88 struct hv_monitor_page *monitorPage;
92 if (Channel->OfferMsg.MonitorAllocated)
94 /* Each u32 represents 32 channels */
95 set_bit(Channel->OfferMsg.ChildRelId & 31,
96 (unsigned long *) gVmbusConnection.SendInterruptPage +
97 (Channel->OfferMsg.ChildRelId >> 5) );
99 monitorPage = (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
100 monitorPage++; /* Get the child to parent monitor page */
102 set_bit(Channel->MonitorBit,
103 (unsigned long *) &monitorPage->TriggerGroup[Channel->MonitorGroup].Pending);
108 VmbusSetEvent(Channel->OfferMsg.ChildRelId);
115 static void VmbusChannelClearEvent(struct vmbus_channel *channel)
117 struct hv_monitor_page *monitorPage;
121 if (Channel->OfferMsg.MonitorAllocated)
123 /* Each u32 represents 32 channels */
124 clear_bit(Channel->OfferMsg.ChildRelId & 31,
125 (unsigned long *) gVmbusConnection.SendInterruptPage + (Channel->OfferMsg.ChildRelId >> 5));
127 monitorPage = (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
128 monitorPage++; /* Get the child to parent monitor page */
130 clear_bit(Channel->MonitorBit,
131 (unsigned long *) &monitorPage->TriggerGroup[Channel->MonitorGroup].Pending);
141 VmbusChannelGetDebugInfo()
144 Retrieve various channel debug info
147 void VmbusChannelGetDebugInfo(struct vmbus_channel *Channel,
148 struct vmbus_channel_debug_info *DebugInfo)
150 struct hv_monitor_page *monitorPage;
151 u8 monitorGroup = (u8)Channel->OfferMsg.MonitorId / 32;
152 u8 monitorOffset = (u8)Channel->OfferMsg.MonitorId % 32;
153 /* u32 monitorBit = 1 << monitorOffset; */
155 DebugInfo->RelId = Channel->OfferMsg.ChildRelId;
156 DebugInfo->State = Channel->State;
157 memcpy(&DebugInfo->InterfaceType, &Channel->OfferMsg.Offer.InterfaceType, sizeof(struct hv_guid));
158 memcpy(&DebugInfo->InterfaceInstance, &Channel->OfferMsg.Offer.InterfaceInstance, sizeof(struct hv_guid));
160 monitorPage = (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
162 DebugInfo->MonitorId = Channel->OfferMsg.MonitorId;
164 DebugInfo->ServerMonitorPending = monitorPage->TriggerGroup[monitorGroup].Pending;
165 DebugInfo->ServerMonitorLatency = monitorPage->Latency[monitorGroup][ monitorOffset];
166 DebugInfo->ServerMonitorConnectionId = monitorPage->Parameter[monitorGroup][ monitorOffset].ConnectionId.u.Id;
170 DebugInfo->ClientMonitorPending = monitorPage->TriggerGroup[monitorGroup].Pending;
171 DebugInfo->ClientMonitorLatency = monitorPage->Latency[monitorGroup][ monitorOffset];
172 DebugInfo->ClientMonitorConnectionId = monitorPage->Parameter[monitorGroup][ monitorOffset].ConnectionId.u.Id;
174 RingBufferGetDebugInfo(&Channel->Inbound, &DebugInfo->Inbound);
175 RingBufferGetDebugInfo(&Channel->Outbound, &DebugInfo->Outbound);
185 Open the specified channel.
188 int VmbusChannelOpen(struct vmbus_channel *NewChannel,
189 u32 SendRingBufferSize,
190 u32 RecvRingBufferSize,
193 PFN_CHANNEL_CALLBACK pfnOnChannelCallback,
198 struct vmbus_channel_open_channel *openMsg;
199 struct vmbus_channel_msginfo *openInfo;
205 /* Aligned to page size */
206 ASSERT(!(SendRingBufferSize & (PAGE_SIZE -1)));
207 ASSERT(!(RecvRingBufferSize & (PAGE_SIZE -1)));
209 NewChannel->OnChannelCallback = pfnOnChannelCallback;
210 NewChannel->ChannelCallbackContext = Context;
212 /* Allocate the ring buffer */
213 out = osd_PageAlloc((SendRingBufferSize + RecvRingBufferSize) >> PAGE_SHIFT);
214 /* out = kzalloc(sendRingBufferSize + recvRingBufferSize, GFP_KERNEL); */
216 ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0);
218 in = (void*)((unsigned long)out + SendRingBufferSize);
220 NewChannel->RingBufferPages = out;
221 NewChannel->RingBufferPageCount = (SendRingBufferSize + RecvRingBufferSize) >> PAGE_SHIFT;
223 RingBufferInit(&NewChannel->Outbound, out, SendRingBufferSize);
225 RingBufferInit(&NewChannel->Inbound, in, RecvRingBufferSize);
227 /* Establish the gpadl for the ring buffer */
228 DPRINT_DBG(VMBUS, "Establishing ring buffer's gpadl for channel %p...", NewChannel);
230 NewChannel->RingBufferGpadlHandle = 0;
232 ret = VmbusChannelEstablishGpadl(NewChannel,
233 NewChannel->Outbound.RingBuffer,
234 SendRingBufferSize + RecvRingBufferSize,
235 &NewChannel->RingBufferGpadlHandle);
237 DPRINT_DBG(VMBUS, "channel %p <relid %d gpadl 0x%x send ring %p size %d recv ring %p size %d, downstreamoffset %d>",
239 NewChannel->OfferMsg.ChildRelId,
240 NewChannel->RingBufferGpadlHandle,
241 NewChannel->Outbound.RingBuffer,
242 NewChannel->Outbound.RingSize,
243 NewChannel->Inbound.RingBuffer,
244 NewChannel->Inbound.RingSize,
247 /* Create and init the channel open message */
248 openInfo = kmalloc(sizeof(*openInfo) + sizeof(struct vmbus_channel_open_channel), GFP_KERNEL);
249 ASSERT(openInfo != NULL);
251 openInfo->WaitEvent = osd_WaitEventCreate();
253 openMsg = (struct vmbus_channel_open_channel *)openInfo->Msg;
254 openMsg->Header.MessageType = ChannelMessageOpenChannel;
255 openMsg->OpenId = NewChannel->OfferMsg.ChildRelId; /* FIXME */
256 openMsg->ChildRelId = NewChannel->OfferMsg.ChildRelId;
257 openMsg->RingBufferGpadlHandle = NewChannel->RingBufferGpadlHandle;
258 ASSERT(openMsg->RingBufferGpadlHandle);
259 openMsg->DownstreamRingBufferPageOffset = SendRingBufferSize >> PAGE_SHIFT;
260 openMsg->ServerContextAreaGpadlHandle = 0; /* TODO */
262 ASSERT(UserDataLen <= MAX_USER_DEFINED_BYTES);
265 memcpy(openMsg->UserData, UserData, UserDataLen);
268 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
269 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &openInfo->MsgListEntry);
270 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
272 DPRINT_DBG(VMBUS, "Sending channel open msg...");
274 ret = VmbusPostMessage(openMsg, sizeof(struct vmbus_channel_open_channel));
277 DPRINT_ERR(VMBUS, "unable to open channel - %d", ret);
281 /* FIXME: Need to time-out here */
282 osd_WaitEventWait(openInfo->WaitEvent);
284 if (openInfo->Response.OpenResult.Status == 0)
286 DPRINT_INFO(VMBUS, "channel <%p> open success!!", NewChannel);
290 DPRINT_INFO(VMBUS, "channel <%p> open failed - %d!!", NewChannel, openInfo->Response.OpenResult.Status);
294 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
295 REMOVE_ENTRY_LIST(&openInfo->MsgListEntry);
296 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
298 kfree(openInfo->WaitEvent);
312 Dump the gpadl body message to the console for debugging purposes.
315 static void DumpGpadlBody(struct vmbus_channel_gpadl_body *Gpadl, u32 Len)
320 pfnCount = (Len - sizeof(struct vmbus_channel_gpadl_body))/ sizeof(u64);
321 DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", Len, pfnCount);
323 for (i=0; i< pfnCount; i++)
325 DPRINT_DBG(VMBUS, "gpadl body - %d) pfn %llu", i, Gpadl->Pfn[i]);
336 Dump the gpadl header message to the console for debugging purposes.
339 static void DumpGpadlHeader(struct vmbus_channel_gpadl_header *Gpadl)
345 DPRINT_DBG(VMBUS, "gpadl header - relid %d, range count %d, range buflen %d",
349 for (i=0; i< Gpadl->RangeCount; i++)
351 pageCount = Gpadl->Range[i].ByteCount >> PAGE_SHIFT;
352 pageCount = (pageCount > 26)? 26 : pageCount;
354 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d page count %d",
355 i, Gpadl->Range[i].ByteCount, Gpadl->Range[i].ByteOffset, pageCount);
357 for (j=0; j< pageCount; j++)
359 DPRINT_DBG(VMBUS, "%d) pfn %llu", j, Gpadl->Range[i].PfnArray[j]);
367 VmbusChannelCreateGpadlHeader()
370 Creates a gpadl for the specified buffer
374 VmbusChannelCreateGpadlHeader(
375 void * Kbuffer, /* from kmalloc() */
376 u32 Size, /* page-size multiple */
377 struct vmbus_channel_msginfo **MsgInfo,
382 unsigned long long pfn;
383 struct vmbus_channel_gpadl_header *gpaHeader;
384 struct vmbus_channel_gpadl_body *gpadlBody;
385 struct vmbus_channel_msginfo *msgHeader;
386 struct vmbus_channel_msginfo *msgBody;
389 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
391 /* ASSERT( (kbuffer & (PAGE_SIZE-1)) == 0); */
392 ASSERT( (Size & (PAGE_SIZE-1)) == 0);
394 pageCount = Size >> PAGE_SHIFT;
395 pfn = virt_to_phys(Kbuffer) >> PAGE_SHIFT;
397 /* do we need a gpadl body msg */
398 pfnSize = MAX_SIZE_CHANNEL_MESSAGE - sizeof(struct vmbus_channel_gpadl_header) - sizeof(struct gpa_range);
399 pfnCount = pfnSize / sizeof(u64);
401 if (pageCount > pfnCount) /* we need a gpadl body */
403 /* fill in the header */
404 msgSize = sizeof(struct vmbus_channel_msginfo) + sizeof(struct vmbus_channel_gpadl_header) + sizeof(struct gpa_range) + pfnCount*sizeof(u64);
405 msgHeader = kzalloc(msgSize, GFP_KERNEL);
407 INITIALIZE_LIST_HEAD(&msgHeader->SubMsgList);
408 msgHeader->MessageSize=msgSize;
410 gpaHeader = (struct vmbus_channel_gpadl_header *)msgHeader->Msg;
411 gpaHeader->RangeCount = 1;
412 gpaHeader->RangeBufLen = sizeof(struct gpa_range) + pageCount*sizeof(u64);
413 gpaHeader->Range[0].ByteOffset = 0;
414 gpaHeader->Range[0].ByteCount = Size;
415 for (i=0; i<pfnCount; i++)
417 gpaHeader->Range[0].PfnArray[i] = pfn+i;
419 *MsgInfo = msgHeader;
423 pfnLeft = pageCount - pfnCount;
425 /* how many pfns can we fit */
426 pfnSize = MAX_SIZE_CHANNEL_MESSAGE - sizeof(struct vmbus_channel_gpadl_body);
427 pfnCount = pfnSize / sizeof(u64);
429 /* fill in the body */
432 if (pfnLeft > pfnCount)
441 msgSize = sizeof(struct vmbus_channel_msginfo) + sizeof(struct vmbus_channel_gpadl_body) + pfnCurr*sizeof(u64);
442 msgBody = kzalloc(msgSize, GFP_KERNEL);
444 msgBody->MessageSize = msgSize;
446 gpadlBody = (struct vmbus_channel_gpadl_body *)msgBody->Msg;
448 /* FIXME: Gpadl is u32 and we are using a pointer which could be 64-bit */
449 /* gpadlBody->Gpadl = kbuffer; */
450 for (i=0; i<pfnCurr; i++)
452 gpadlBody->Pfn[i] = pfn + pfnSum + i;
455 /* add to msg header */
456 INSERT_TAIL_LIST(&msgHeader->SubMsgList, &msgBody->MsgListEntry);
463 /* everything fits in a header */
464 msgSize = sizeof(struct vmbus_channel_msginfo) + sizeof(struct vmbus_channel_gpadl_header) + sizeof(struct gpa_range) + pageCount*sizeof(u64);
465 msgHeader = kzalloc(msgSize, GFP_KERNEL);
466 msgHeader->MessageSize=msgSize;
468 gpaHeader = (struct vmbus_channel_gpadl_header *)msgHeader->Msg;
469 gpaHeader->RangeCount = 1;
470 gpaHeader->RangeBufLen = sizeof(struct gpa_range) + pageCount*sizeof(u64);
471 gpaHeader->Range[0].ByteOffset = 0;
472 gpaHeader->Range[0].ByteCount = Size;
473 for (i=0; i<pageCount; i++)
475 gpaHeader->Range[0].PfnArray[i] = pfn+i;
478 *MsgInfo = msgHeader;
489 VmbusChannelEstablishGpadl()
492 Estabish a GPADL for the specified buffer
495 int VmbusChannelEstablishGpadl(struct vmbus_channel *Channel,
496 void * Kbuffer, /* from kmalloc() */
497 u32 Size, /* page-size multiple */
502 struct vmbus_channel_gpadl_header *gpadlMsg;
503 struct vmbus_channel_gpadl_body *gpadlBody;
504 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
506 struct vmbus_channel_msginfo *msgInfo;
507 struct vmbus_channel_msginfo *subMsgInfo;
517 nextGpadlHandle = atomic_read(&gVmbusConnection.NextGpadlHandle);
518 atomic_inc(&gVmbusConnection.NextGpadlHandle);
520 VmbusChannelCreateGpadlHeader(Kbuffer, Size, &msgInfo, &msgCount);
521 ASSERT(msgInfo != NULL);
524 msgInfo->WaitEvent = osd_WaitEventCreate();
525 gpadlMsg = (struct vmbus_channel_gpadl_header *)msgInfo->Msg;
526 gpadlMsg->Header.MessageType = ChannelMessageGpadlHeader;
527 gpadlMsg->ChildRelId = Channel->OfferMsg.ChildRelId;
528 gpadlMsg->Gpadl = nextGpadlHandle;
530 DumpGpadlHeader(gpadlMsg);
532 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
533 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &msgInfo->MsgListEntry);
534 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
536 DPRINT_DBG(VMBUS, "buffer %p, size %d msg cnt %d", Kbuffer, Size, msgCount);
538 DPRINT_DBG(VMBUS, "Sending GPADL Header - len %zd", msgInfo->MessageSize - sizeof(*msgInfo));
540 ret = VmbusPostMessage(gpadlMsg, msgInfo->MessageSize - sizeof(*msgInfo));
543 DPRINT_ERR(VMBUS, "Unable to open channel - %d", ret);
549 ITERATE_LIST_ENTRIES(anchor, curr, &msgInfo->SubMsgList)
551 subMsgInfo = (struct vmbus_channel_msginfo *)curr;
552 gpadlBody = (struct vmbus_channel_gpadl_body *)subMsgInfo->Msg;
554 gpadlBody->Header.MessageType = ChannelMessageGpadlBody;
555 gpadlBody->Gpadl = nextGpadlHandle;
557 DPRINT_DBG(VMBUS, "Sending GPADL Body - len %zd", subMsgInfo->MessageSize - sizeof(*subMsgInfo));
559 DumpGpadlBody(gpadlBody, subMsgInfo->MessageSize - sizeof(*subMsgInfo));
560 ret = VmbusPostMessage(gpadlBody, subMsgInfo->MessageSize - sizeof(*subMsgInfo));
564 osd_WaitEventWait(msgInfo->WaitEvent);
566 /* At this point, we received the gpadl created msg */
567 DPRINT_DBG(VMBUS, "Received GPADL created (relid %d, status %d handle %x)",
568 Channel->OfferMsg.ChildRelId,
569 msgInfo->Response.GpadlCreated.CreationStatus,
572 *GpadlHandle = gpadlMsg->Gpadl;
575 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
576 REMOVE_ENTRY_LIST(&msgInfo->MsgListEntry);
577 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
579 kfree(msgInfo->WaitEvent);
592 VmbusChannelTeardownGpadl()
595 Teardown the specified GPADL handle
598 int VmbusChannelTeardownGpadl(struct vmbus_channel *Channel, u32 GpadlHandle)
601 struct vmbus_channel_gpadl_teardown *msg;
602 struct vmbus_channel_msginfo *info;
607 ASSERT(GpadlHandle != 0);
609 info = kmalloc(sizeof(*info) + sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
610 ASSERT(info != NULL);
612 info->WaitEvent = osd_WaitEventCreate();
614 msg = (struct vmbus_channel_gpadl_teardown *)info->Msg;
616 msg->Header.MessageType = ChannelMessageGpadlTeardown;
617 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
618 msg->Gpadl = GpadlHandle;
620 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
621 INSERT_TAIL_LIST(&gVmbusConnection.ChannelMsgList, &info->MsgListEntry);
622 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
624 ret = VmbusPostMessage(msg, sizeof(struct vmbus_channel_gpadl_teardown));
630 osd_WaitEventWait(info->WaitEvent);
632 /* Received a torndown response */
633 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
634 REMOVE_ENTRY_LIST(&info->MsgListEntry);
635 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
637 kfree(info->WaitEvent);
652 Close the specified channel
655 void VmbusChannelClose(struct vmbus_channel *Channel)
658 struct vmbus_channel_close_channel *msg;
659 struct vmbus_channel_msginfo *info;
664 /* Stop callback and cancel the timer asap */
665 Channel->OnChannelCallback = NULL;
666 del_timer(&Channel->poll_timer);
668 /* Send a closing message */
669 info = kmalloc(sizeof(*info) + sizeof(struct vmbus_channel_close_channel), GFP_KERNEL);
670 ASSERT(info != NULL);
672 /* info->waitEvent = osd_WaitEventCreate(); */
674 msg = (struct vmbus_channel_close_channel *)info->Msg;
675 msg->Header.MessageType = ChannelMessageCloseChannel;
676 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
678 ret = VmbusPostMessage(msg, sizeof(struct vmbus_channel_close_channel));
684 /* Tear down the gpadl for the channel's ring buffer */
685 if (Channel->RingBufferGpadlHandle)
687 VmbusChannelTeardownGpadl(Channel, Channel->RingBufferGpadlHandle);
690 /* TODO: Send a msg to release the childRelId */
692 /* Cleanup the ring buffers for this channel */
693 RingBufferCleanup(&Channel->Outbound);
694 RingBufferCleanup(&Channel->Inbound);
696 osd_PageFree(Channel->RingBufferPages, Channel->RingBufferPageCount);
702 * If we are closing the channel during an error path in
703 * opening the channel, don't free the channel since the
704 * caller will free the channel
707 if (Channel->State == CHANNEL_OPEN_STATE)
709 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
710 REMOVE_ENTRY_LIST(&Channel->ListEntry);
711 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
713 FreeVmbusChannel(Channel);
723 VmbusChannelSendPacket()
726 Send the specified buffer on the given channel
729 int VmbusChannelSendPacket(struct vmbus_channel *Channel,
733 enum vmbus_packet_type Type,
738 struct vmpacket_descriptor desc;
739 u32 packetLen = sizeof(struct vmpacket_descriptor) + BufferLen;
740 u32 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
741 struct scatterlist bufferList[3];
745 DPRINT_DBG(VMBUS, "channel %p buffer %p len %d", Channel, Buffer, BufferLen);
747 DumpVmbusChannel(Channel);
749 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
751 /* Setup the descriptor */
752 desc.Type = Type; /* VmbusPacketTypeDataInBand; */
753 desc.Flags = Flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
754 desc.DataOffset8 = sizeof(struct vmpacket_descriptor) >> 3; /* in 8-bytes granularity */
755 desc.Length8 = (u16)(packetLenAligned >> 3);
756 desc.TransactionId = RequestId;
758 sg_init_table(bufferList,3);
759 sg_set_buf(&bufferList[0], &desc, sizeof(struct vmpacket_descriptor));
760 sg_set_buf(&bufferList[1], Buffer, BufferLen);
761 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
763 ret = RingBufferWrite(
768 /* TODO: We should determine if this is optional */
769 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
771 VmbusChannelSetEvent(Channel);
783 VmbusChannelSendPacketPageBuffer()
786 Send a range of single-page buffer packets using a GPADL Direct packet type.
789 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel *Channel,
790 struct hv_page_buffer PageBuffers[],
799 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc;
802 u32 packetLenAligned;
803 struct scatterlist bufferList[3];
808 ASSERT(PageCount <= MAX_PAGE_BUFFER_COUNT);
810 DumpVmbusChannel(Channel);
812 /* Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the largest size we support */
813 descSize = sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER) - ((MAX_PAGE_BUFFER_COUNT - PageCount)*sizeof(struct hv_page_buffer));
814 packetLen = descSize + BufferLen;
815 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
817 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
819 /* Setup the descriptor */
820 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
821 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
822 desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
823 desc.Length8 = (u16)(packetLenAligned >> 3);
824 desc.TransactionId = RequestId;
825 desc.RangeCount = PageCount;
827 for (i=0; i<PageCount; i++)
829 desc.Range[i].Length = PageBuffers[i].Length;
830 desc.Range[i].Offset = PageBuffers[i].Offset;
831 desc.Range[i].Pfn = PageBuffers[i].Pfn;
834 sg_init_table(bufferList,3);
835 sg_set_buf(&bufferList[0], &desc, descSize);
836 sg_set_buf(&bufferList[1], Buffer, BufferLen);
837 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
839 ret = RingBufferWrite(
844 /* TODO: We should determine if this is optional */
845 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
847 VmbusChannelSetEvent(Channel);
860 VmbusChannelSendPacketMultiPageBuffer()
863 Send a multi-page buffer packet using a GPADL Direct packet type.
866 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel *Channel,
867 struct hv_multipage_buffer *MultiPageBuffer,
874 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc;
877 u32 packetLenAligned;
878 struct scatterlist bufferList[3];
880 u32 PfnCount = NUM_PAGES_SPANNED(MultiPageBuffer->Offset, MultiPageBuffer->Length);
884 DumpVmbusChannel(Channel);
886 DPRINT_DBG(VMBUS, "data buffer - offset %u len %u pfn count %u", MultiPageBuffer->Offset, MultiPageBuffer->Length, PfnCount);
888 ASSERT(PfnCount > 0);
889 ASSERT(PfnCount <= MAX_MULTIPAGE_BUFFER_COUNT);
891 /* Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is the largest size we support */
892 descSize = sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER) - ((MAX_MULTIPAGE_BUFFER_COUNT - PfnCount)*sizeof(u64));
893 packetLen = descSize + BufferLen;
894 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
896 ASSERT((packetLenAligned - packetLen) < sizeof(u64));
898 /* Setup the descriptor */
899 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
900 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
901 desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
902 desc.Length8 = (u16)(packetLenAligned >> 3);
903 desc.TransactionId = RequestId;
906 desc.Range.Length = MultiPageBuffer->Length;
907 desc.Range.Offset = MultiPageBuffer->Offset;
909 memcpy(desc.Range.PfnArray, MultiPageBuffer->PfnArray, PfnCount*sizeof(u64));
911 sg_init_table(bufferList,3);
912 sg_set_buf(&bufferList[0], &desc, descSize);
913 sg_set_buf(&bufferList[1], Buffer, BufferLen);
914 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
916 ret = RingBufferWrite(
921 /* TODO: We should determine if this is optional */
922 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
924 VmbusChannelSetEvent(Channel);
936 VmbusChannelRecvPacket()
939 Retrieve the user packet on the specified channel
942 /* TODO: Do we ever receive a gpa direct packet other than the ones we send ? */
943 int VmbusChannelRecvPacket(struct vmbus_channel *Channel,
946 u32 *BufferActualLen,
949 struct vmpacket_descriptor desc;
957 *BufferActualLen = 0;
960 spin_lock_irqsave(&Channel->inbound_lock, flags);
962 ret = RingBufferPeek(&Channel->Inbound, &desc, sizeof(struct vmpacket_descriptor));
965 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
967 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
972 /* VmbusChannelClearEvent(Channel); */
974 packetLen = desc.Length8 << 3;
975 userLen = packetLen - (desc.DataOffset8 << 3);
976 /* ASSERT(userLen > 0); */
978 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
980 Channel->OfferMsg.ChildRelId,
983 desc.TransactionId, packetLen, userLen);
985 *BufferActualLen = userLen;
987 if (userLen > BufferLen)
989 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
991 DPRINT_ERR(VMBUS, "buffer too small - got %d needs %d", BufferLen, userLen);
997 *RequestId = desc.TransactionId;
999 /* Copy over the packet to the user buffer */
1000 ret = RingBufferRead(&Channel->Inbound, Buffer, userLen, (desc.DataOffset8 << 3));
1002 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1012 VmbusChannelRecvPacketRaw()
1015 Retrieve the raw packet on the specified channel
1018 int VmbusChannelRecvPacketRaw(struct vmbus_channel *Channel,
1021 u32* BufferActualLen,
1025 struct vmpacket_descriptor desc;
1029 unsigned long flags;
1031 DPRINT_ENTER(VMBUS);
1033 *BufferActualLen = 0;
1036 spin_lock_irqsave(&Channel->inbound_lock, flags);
1038 ret = RingBufferPeek(&Channel->Inbound, &desc, sizeof(struct vmpacket_descriptor));
1041 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1043 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1048 /* VmbusChannelClearEvent(Channel); */
1050 packetLen = desc.Length8 << 3;
1051 userLen = packetLen - (desc.DataOffset8 << 3);
1053 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d flag %d tid %llx pktlen %d datalen %d> ",
1055 Channel->OfferMsg.ChildRelId,
1058 desc.TransactionId, packetLen, userLen);
1060 *BufferActualLen = packetLen;
1062 if (packetLen > BufferLen)
1064 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1066 DPRINT_ERR(VMBUS, "buffer too small - needed %d bytes but got space for only %d bytes", packetLen, BufferLen);
1071 *RequestId = desc.TransactionId;
1073 /* Copy over the entire packet to the user buffer */
1074 ret = RingBufferRead(&Channel->Inbound, Buffer, packetLen, 0);
1076 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1087 VmbusChannelOnChannelEvent()
1090 Channel event callback
1093 void VmbusChannelOnChannelEvent(struct vmbus_channel *Channel)
1095 DumpVmbusChannel(Channel);
1096 ASSERT(Channel->OnChannelCallback);
1097 #ifdef ENABLE_POLLING
1098 del_timer(&Channel->poll_timer);
1099 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1100 channel->poll_timer.expires(jiffies + usecs_to_jiffies(100);
1101 add_timer(&channel->poll_timer);
1103 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1110 VmbusChannelOnTimer()
1113 Timer event callback
1116 void VmbusChannelOnTimer(unsigned long data)
1118 struct vmbus_channel *channel = (struct vmbus_channel *)data;
1120 if (channel->OnChannelCallback)
1122 channel->OnChannelCallback(channel->ChannelCallbackContext);
1123 #ifdef ENABLE_POLLING
1124 channel->poll_timer.expires(jiffies + usecs_to_jiffies(100);
1125 add_timer(&channel->poll_timer);
1137 Dump vmbus channel info to the console
1140 static void DumpVmbusChannel(struct vmbus_channel *Channel)
1142 DPRINT_DBG(VMBUS, "Channel (%d)", Channel->OfferMsg.ChildRelId);
1143 DumpRingInfo(&Channel->Outbound, "Outbound ");
1144 DumpRingInfo(&Channel->Inbound, "Inbound ");