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/module.h>
27 #include "vmbus_private.h"
29 /* Internal routines */
30 static int VmbusChannelCreateGpadlHeader(
31 void *Kbuffer, /* must be phys and virt contiguous */
32 u32 Size, /* page-size multiple */
33 struct vmbus_channel_msginfo **msgInfo,
35 static void DumpVmbusChannel(struct vmbus_channel *channel);
36 static void VmbusChannelSetEvent(struct vmbus_channel *channel);
40 static void DumpMonitorPage(struct hv_monitor_page *MonitorPage)
45 DPRINT_DBG(VMBUS, "monitorPage - %p, trigger state - %d",
46 MonitorPage, MonitorPage->TriggerState);
48 for (i = 0; i < 4; i++)
49 DPRINT_DBG(VMBUS, "trigger group (%d) - %llx", i,
50 MonitorPage->TriggerGroup[i].AsUINT64);
52 for (i = 0; i < 4; i++) {
53 for (j = 0; j < 32; j++) {
54 DPRINT_DBG(VMBUS, "latency (%d)(%d) - %llx", i, j,
55 MonitorPage->Latency[i][j]);
58 for (i = 0; i < 4; i++) {
59 for (j = 0; j < 32; j++) {
60 DPRINT_DBG(VMBUS, "param-conn id (%d)(%d) - %d", i, j,
61 MonitorPage->Parameter[i][j].ConnectionId.Asu32);
62 DPRINT_DBG(VMBUS, "param-flag (%d)(%d) - %d", i, j,
63 MonitorPage->Parameter[i][j].FlagNumber);
70 * VmbusChannelSetEvent - Trigger an event notification on the specified
73 static void VmbusChannelSetEvent(struct vmbus_channel *Channel)
75 struct hv_monitor_page *monitorPage;
77 if (Channel->OfferMsg.MonitorAllocated) {
78 /* Each u32 represents 32 channels */
79 set_bit(Channel->OfferMsg.ChildRelId & 31,
80 (unsigned long *) gVmbusConnection.SendInterruptPage +
81 (Channel->OfferMsg.ChildRelId >> 5));
83 monitorPage = gVmbusConnection.MonitorPages;
84 monitorPage++; /* Get the child to parent monitor page */
86 set_bit(Channel->MonitorBit,
87 (unsigned long *)&monitorPage->TriggerGroup
88 [Channel->MonitorGroup].Pending);
91 VmbusSetEvent(Channel->OfferMsg.ChildRelId);
98 static void VmbusChannelClearEvent(struct vmbus_channel *channel)
100 struct hv_monitor_page *monitorPage;
102 if (Channel->OfferMsg.MonitorAllocated) {
103 /* Each u32 represents 32 channels */
104 clear_bit(Channel->OfferMsg.ChildRelId & 31,
105 (unsigned long *)gVmbusConnection.SendInterruptPage +
106 (Channel->OfferMsg.ChildRelId >> 5));
109 (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
110 monitorPage++; /* Get the child to parent monitor page */
112 clear_bit(Channel->MonitorBit,
113 (unsigned long *)&monitorPage->TriggerGroup
114 [Channel->MonitorGroup].Pending);
122 * VmbusChannelGetDebugInfo -Retrieve various channel debug info
124 void VmbusChannelGetDebugInfo(struct vmbus_channel *Channel,
125 struct vmbus_channel_debug_info *DebugInfo)
127 struct hv_monitor_page *monitorPage;
128 u8 monitorGroup = (u8)Channel->OfferMsg.MonitorId / 32;
129 u8 monitorOffset = (u8)Channel->OfferMsg.MonitorId % 32;
130 /* u32 monitorBit = 1 << monitorOffset; */
132 DebugInfo->RelId = Channel->OfferMsg.ChildRelId;
133 DebugInfo->State = Channel->State;
134 memcpy(&DebugInfo->InterfaceType,
135 &Channel->OfferMsg.Offer.InterfaceType, sizeof(struct hv_guid));
136 memcpy(&DebugInfo->InterfaceInstance,
137 &Channel->OfferMsg.Offer.InterfaceInstance,
138 sizeof(struct hv_guid));
140 monitorPage = (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
142 DebugInfo->MonitorId = Channel->OfferMsg.MonitorId;
144 DebugInfo->ServerMonitorPending =
145 monitorPage->TriggerGroup[monitorGroup].Pending;
146 DebugInfo->ServerMonitorLatency =
147 monitorPage->Latency[monitorGroup][monitorOffset];
148 DebugInfo->ServerMonitorConnectionId =
149 monitorPage->Parameter[monitorGroup]
150 [monitorOffset].ConnectionId.u.Id;
154 DebugInfo->ClientMonitorPending =
155 monitorPage->TriggerGroup[monitorGroup].Pending;
156 DebugInfo->ClientMonitorLatency =
157 monitorPage->Latency[monitorGroup][monitorOffset];
158 DebugInfo->ClientMonitorConnectionId =
159 monitorPage->Parameter[monitorGroup]
160 [monitorOffset].ConnectionId.u.Id;
162 RingBufferGetDebugInfo(&Channel->Inbound, &DebugInfo->Inbound);
163 RingBufferGetDebugInfo(&Channel->Outbound, &DebugInfo->Outbound);
167 * VmbusChannelOpen - Open the specified channel.
169 int VmbusChannelOpen(struct vmbus_channel *NewChannel, u32 SendRingBufferSize,
170 u32 RecvRingBufferSize, void *UserData, u32 UserDataLen,
171 void (*OnChannelCallback)(void *context), void *Context)
173 struct vmbus_channel_open_channel *openMsg;
174 struct vmbus_channel_msginfo *openInfo = NULL;
179 /* Aligned to page size */
180 /* ASSERT(!(SendRingBufferSize & (PAGE_SIZE - 1))); */
181 /* ASSERT(!(RecvRingBufferSize & (PAGE_SIZE - 1))); */
183 NewChannel->OnChannelCallback = OnChannelCallback;
184 NewChannel->ChannelCallbackContext = Context;
186 /* Allocate the ring buffer */
187 out = osd_PageAlloc((SendRingBufferSize + RecvRingBufferSize)
192 /* ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0); */
194 in = (void *)((unsigned long)out + SendRingBufferSize);
196 NewChannel->RingBufferPages = out;
197 NewChannel->RingBufferPageCount = (SendRingBufferSize +
198 RecvRingBufferSize) >> PAGE_SHIFT;
200 ret = RingBufferInit(&NewChannel->Outbound, out, SendRingBufferSize);
206 ret = RingBufferInit(&NewChannel->Inbound, in, RecvRingBufferSize);
213 /* Establish the gpadl for the ring buffer */
214 DPRINT_DBG(VMBUS, "Establishing ring buffer's gpadl for channel %p...",
217 NewChannel->RingBufferGpadlHandle = 0;
219 ret = VmbusChannelEstablishGpadl(NewChannel,
220 NewChannel->Outbound.RingBuffer,
223 &NewChannel->RingBufferGpadlHandle);
230 DPRINT_DBG(VMBUS, "channel %p <relid %d gpadl 0x%x send ring %p "
231 "size %d recv ring %p size %d, downstreamoffset %d>",
232 NewChannel, NewChannel->OfferMsg.ChildRelId,
233 NewChannel->RingBufferGpadlHandle,
234 NewChannel->Outbound.RingBuffer,
235 NewChannel->Outbound.RingSize,
236 NewChannel->Inbound.RingBuffer,
237 NewChannel->Inbound.RingSize,
240 /* Create and init the channel open message */
241 openInfo = kmalloc(sizeof(*openInfo) +
242 sizeof(struct vmbus_channel_open_channel),
249 openInfo->WaitEvent = osd_WaitEventCreate();
250 if (!openInfo->WaitEvent) {
255 openMsg = (struct vmbus_channel_open_channel *)openInfo->Msg;
256 openMsg->Header.MessageType = ChannelMessageOpenChannel;
257 openMsg->OpenId = NewChannel->OfferMsg.ChildRelId; /* FIXME */
258 openMsg->ChildRelId = NewChannel->OfferMsg.ChildRelId;
259 openMsg->RingBufferGpadlHandle = NewChannel->RingBufferGpadlHandle;
260 openMsg->DownstreamRingBufferPageOffset = SendRingBufferSize >>
262 openMsg->ServerContextAreaGpadlHandle = 0; /* TODO */
264 if (UserDataLen > MAX_USER_DEFINED_BYTES) {
270 memcpy(openMsg->UserData, UserData, UserDataLen);
272 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
273 list_add_tail(&openInfo->MsgListEntry,
274 &gVmbusConnection.ChannelMsgList);
275 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
277 DPRINT_DBG(VMBUS, "Sending channel open msg...");
279 ret = VmbusPostMessage(openMsg,
280 sizeof(struct vmbus_channel_open_channel));
282 DPRINT_ERR(VMBUS, "unable to open channel - %d", ret);
286 /* FIXME: Need to time-out here */
287 osd_WaitEventWait(openInfo->WaitEvent);
289 if (openInfo->Response.OpenResult.Status == 0)
290 DPRINT_INFO(VMBUS, "channel <%p> open success!!", NewChannel);
292 DPRINT_INFO(VMBUS, "channel <%p> open failed - %d!!",
293 NewChannel, openInfo->Response.OpenResult.Status);
296 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
297 list_del(&openInfo->MsgListEntry);
298 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
300 kfree(openInfo->WaitEvent);
308 RingBufferCleanup(&NewChannel->Outbound);
309 RingBufferCleanup(&NewChannel->Inbound);
310 osd_PageFree(out, (SendRingBufferSize + RecvRingBufferSize)
317 * DumpGpadlBody - Dump the gpadl body message to the console for
318 * debugging purposes.
320 static void DumpGpadlBody(struct vmbus_channel_gpadl_body *Gpadl, u32 Len)
325 pfnCount = (Len - sizeof(struct vmbus_channel_gpadl_body)) /
327 DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", Len, pfnCount);
329 for (i = 0; i < pfnCount; i++)
330 DPRINT_DBG(VMBUS, "gpadl body - %d) pfn %llu",
335 * DumpGpadlHeader - Dump the gpadl header message to the console for
336 * debugging purposes.
338 static void DumpGpadlHeader(struct vmbus_channel_gpadl_header *Gpadl)
344 "gpadl header - relid %d, range count %d, range buflen %d",
345 Gpadl->ChildRelId, Gpadl->RangeCount, Gpadl->RangeBufLen);
346 for (i = 0; i < Gpadl->RangeCount; i++) {
347 pageCount = Gpadl->Range[i].ByteCount >> PAGE_SHIFT;
348 pageCount = (pageCount > 26) ? 26 : pageCount;
350 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d "
351 "page count %d", i, Gpadl->Range[i].ByteCount,
352 Gpadl->Range[i].ByteOffset, pageCount);
354 for (j = 0; j < pageCount; j++)
355 DPRINT_DBG(VMBUS, "%d) pfn %llu", j,
356 Gpadl->Range[i].PfnArray[j]);
361 * VmbusChannelCreateGpadlHeader - Creates a gpadl for the specified buffer
363 static int VmbusChannelCreateGpadlHeader(void *Kbuffer, u32 Size,
364 struct vmbus_channel_msginfo **MsgInfo,
369 unsigned long long pfn;
370 struct vmbus_channel_gpadl_header *gpaHeader;
371 struct vmbus_channel_gpadl_body *gpadlBody;
372 struct vmbus_channel_msginfo *msgHeader;
373 struct vmbus_channel_msginfo *msgBody = NULL;
376 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
378 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
379 /* ASSERT((Size & (PAGE_SIZE-1)) == 0); */
381 pageCount = Size >> PAGE_SHIFT;
382 pfn = virt_to_phys(Kbuffer) >> PAGE_SHIFT;
384 /* do we need a gpadl body msg */
385 pfnSize = MAX_SIZE_CHANNEL_MESSAGE -
386 sizeof(struct vmbus_channel_gpadl_header) -
387 sizeof(struct gpa_range);
388 pfnCount = pfnSize / sizeof(u64);
390 if (pageCount > pfnCount) {
391 /* we need a gpadl body */
392 /* fill in the header */
393 msgSize = sizeof(struct vmbus_channel_msginfo) +
394 sizeof(struct vmbus_channel_gpadl_header) +
395 sizeof(struct gpa_range) + pfnCount * sizeof(u64);
396 msgHeader = kzalloc(msgSize, GFP_KERNEL);
400 INIT_LIST_HEAD(&msgHeader->SubMsgList);
401 msgHeader->MessageSize = msgSize;
403 gpaHeader = (struct vmbus_channel_gpadl_header *)msgHeader->Msg;
404 gpaHeader->RangeCount = 1;
405 gpaHeader->RangeBufLen = sizeof(struct gpa_range) +
406 pageCount * sizeof(u64);
407 gpaHeader->Range[0].ByteOffset = 0;
408 gpaHeader->Range[0].ByteCount = Size;
409 for (i = 0; i < pfnCount; i++)
410 gpaHeader->Range[0].PfnArray[i] = pfn+i;
411 *MsgInfo = msgHeader;
415 pfnLeft = pageCount - pfnCount;
417 /* how many pfns can we fit */
418 pfnSize = MAX_SIZE_CHANNEL_MESSAGE -
419 sizeof(struct vmbus_channel_gpadl_body);
420 pfnCount = pfnSize / sizeof(u64);
422 /* fill in the body */
424 if (pfnLeft > pfnCount)
429 msgSize = sizeof(struct vmbus_channel_msginfo) +
430 sizeof(struct vmbus_channel_gpadl_body) +
431 pfnCurr * sizeof(u64);
432 msgBody = kzalloc(msgSize, GFP_KERNEL);
433 /* FIXME: we probably need to more if this fails */
436 msgBody->MessageSize = msgSize;
439 (struct vmbus_channel_gpadl_body *)msgBody->Msg;
443 * Gpadl is u32 and we are using a pointer which could
446 /* gpadlBody->Gpadl = kbuffer; */
447 for (i = 0; i < pfnCurr; i++)
448 gpadlBody->Pfn[i] = pfn + pfnSum + i;
450 /* add to msg header */
451 list_add_tail(&msgBody->MsgListEntry,
452 &msgHeader->SubMsgList);
457 /* everything fits in a header */
458 msgSize = sizeof(struct vmbus_channel_msginfo) +
459 sizeof(struct vmbus_channel_gpadl_header) +
460 sizeof(struct gpa_range) + pageCount * sizeof(u64);
461 msgHeader = kzalloc(msgSize, GFP_KERNEL);
462 if (msgHeader == NULL)
464 msgHeader->MessageSize = msgSize;
466 gpaHeader = (struct vmbus_channel_gpadl_header *)msgHeader->Msg;
467 gpaHeader->RangeCount = 1;
468 gpaHeader->RangeBufLen = sizeof(struct gpa_range) +
469 pageCount * sizeof(u64);
470 gpaHeader->Range[0].ByteOffset = 0;
471 gpaHeader->Range[0].ByteCount = Size;
472 for (i = 0; i < pageCount; i++)
473 gpaHeader->Range[0].PfnArray[i] = pfn+i;
475 *MsgInfo = msgHeader;
487 * VmbusChannelEstablishGpadl - Estabish a GPADL for the specified buffer
489 * @Channel: a channel
490 * @Kbuffer: from kmalloc
491 * @Size: page-size multiple
492 * @GpadlHandle: some funky thing
494 int VmbusChannelEstablishGpadl(struct vmbus_channel *Channel, void *Kbuffer,
495 u32 Size, u32 *GpadlHandle)
497 struct vmbus_channel_gpadl_header *gpadlMsg;
498 struct vmbus_channel_gpadl_body *gpadlBody;
499 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
500 struct vmbus_channel_msginfo *msgInfo = NULL;
501 struct vmbus_channel_msginfo *subMsgInfo;
503 struct list_head *curr;
508 nextGpadlHandle = atomic_read(&gVmbusConnection.NextGpadlHandle);
509 atomic_inc(&gVmbusConnection.NextGpadlHandle);
511 ret = VmbusChannelCreateGpadlHeader(Kbuffer, Size, &msgInfo, &msgCount);
515 msgInfo->WaitEvent = osd_WaitEventCreate();
516 if (!msgInfo->WaitEvent) {
521 gpadlMsg = (struct vmbus_channel_gpadl_header *)msgInfo->Msg;
522 gpadlMsg->Header.MessageType = ChannelMessageGpadlHeader;
523 gpadlMsg->ChildRelId = Channel->OfferMsg.ChildRelId;
524 gpadlMsg->Gpadl = nextGpadlHandle;
526 DumpGpadlHeader(gpadlMsg);
528 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
529 list_add_tail(&msgInfo->MsgListEntry,
530 &gVmbusConnection.ChannelMsgList);
532 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
533 DPRINT_DBG(VMBUS, "buffer %p, size %d msg cnt %d",
534 Kbuffer, Size, msgCount);
536 DPRINT_DBG(VMBUS, "Sending GPADL Header - len %zd",
537 msgInfo->MessageSize - sizeof(*msgInfo));
539 ret = VmbusPostMessage(gpadlMsg, msgInfo->MessageSize -
542 DPRINT_ERR(VMBUS, "Unable to open channel - %d", ret);
547 list_for_each(curr, &msgInfo->SubMsgList) {
549 /* FIXME: should this use list_entry() instead ? */
550 subMsgInfo = (struct vmbus_channel_msginfo *)curr;
552 (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",
558 subMsgInfo->MessageSize -
559 sizeof(*subMsgInfo));
561 DumpGpadlBody(gpadlBody, subMsgInfo->MessageSize -
562 sizeof(*subMsgInfo));
563 ret = VmbusPostMessage(gpadlBody,
564 subMsgInfo->MessageSize -
565 sizeof(*subMsgInfo));
571 osd_WaitEventWait(msgInfo->WaitEvent);
573 /* At this point, we received the gpadl created msg */
574 DPRINT_DBG(VMBUS, "Received GPADL created "
575 "(relid %d, status %d handle %x)",
576 Channel->OfferMsg.ChildRelId,
577 msgInfo->Response.GpadlCreated.CreationStatus,
580 *GpadlHandle = gpadlMsg->Gpadl;
583 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
584 list_del(&msgInfo->MsgListEntry);
585 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
587 kfree(msgInfo->WaitEvent);
596 * VmbusChannelTeardownGpadl -Teardown the specified GPADL handle
598 int VmbusChannelTeardownGpadl(struct vmbus_channel *Channel, u32 GpadlHandle)
600 struct vmbus_channel_gpadl_teardown *msg;
601 struct vmbus_channel_msginfo *info;
605 /* ASSERT(GpadlHandle != 0); */
607 info = kmalloc(sizeof(*info) +
608 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
612 info->WaitEvent = osd_WaitEventCreate();
613 if (!info->WaitEvent) {
618 msg = (struct vmbus_channel_gpadl_teardown *)info->Msg;
620 msg->Header.MessageType = ChannelMessageGpadlTeardown;
621 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
622 msg->Gpadl = GpadlHandle;
624 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
625 list_add_tail(&info->MsgListEntry,
626 &gVmbusConnection.ChannelMsgList);
627 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
629 ret = VmbusPostMessage(msg,
630 sizeof(struct vmbus_channel_gpadl_teardown));
636 osd_WaitEventWait(info->WaitEvent);
638 /* Received a torndown response */
639 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
640 list_del(&info->MsgListEntry);
641 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
643 kfree(info->WaitEvent);
652 * VmbusChannelClose - Close the specified channel
654 void VmbusChannelClose(struct vmbus_channel *Channel)
656 struct vmbus_channel_close_channel *msg;
657 struct vmbus_channel_msginfo *info;
661 /* Stop callback and cancel the timer asap */
662 Channel->OnChannelCallback = NULL;
663 del_timer_sync(&Channel->poll_timer);
665 /* Send a closing message */
666 info = kmalloc(sizeof(*info) +
667 sizeof(struct vmbus_channel_close_channel), GFP_KERNEL);
668 /* FIXME: can't do anything other than return here because the
669 * function is void */
673 /* info->waitEvent = osd_WaitEventCreate(); */
675 msg = (struct vmbus_channel_close_channel *)info->Msg;
676 msg->Header.MessageType = ChannelMessageCloseChannel;
677 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
679 ret = VmbusPostMessage(msg, sizeof(struct vmbus_channel_close_channel));
685 /* Tear down the gpadl for the channel's ring buffer */
686 if (Channel->RingBufferGpadlHandle)
687 VmbusChannelTeardownGpadl(Channel,
688 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);
701 * If we are closing the channel during an error path in
702 * opening the channel, don't free the channel since the
703 * caller will free the channel
706 if (Channel->State == CHANNEL_OPEN_STATE) {
707 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
708 list_del(&Channel->ListEntry);
709 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
711 FreeVmbusChannel(Channel);
718 * VmbusChannelSendPacket() - Send the specified buffer on the given channel
719 * @Channel: Pointer to vmbus_channel structure.
720 * @Buffer: Pointer to the buffer you want to receive the data into.
721 * @BufferLen: Maximum size of what the the buffer will hold
722 * @RequestId: Identifier of the request
723 * @vmbus_packet_type: Type of packet that is being send e.g. negotiate, time
726 * Sends data in @Buffer directly to hyper-v via the vmbus
727 * This will send the data unparsed to hyper-v.
729 * Mainly used by Hyper-V drivers.
731 int VmbusChannelSendPacket(struct vmbus_channel *Channel, const void *Buffer,
732 u32 BufferLen, u64 RequestId,
733 enum vmbus_packet_type Type, u32 Flags)
735 struct vmpacket_descriptor desc;
736 u32 packetLen = sizeof(struct vmpacket_descriptor) + BufferLen;
737 u32 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
738 struct scatterlist bufferList[3];
742 DPRINT_DBG(VMBUS, "channel %p buffer %p len %d",
743 Channel, Buffer, BufferLen);
745 DumpVmbusChannel(Channel);
747 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
749 /* Setup the descriptor */
750 desc.Type = Type; /* VmbusPacketTypeDataInBand; */
751 desc.Flags = Flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
752 /* in 8-bytes granularity */
753 desc.DataOffset8 = sizeof(struct vmpacket_descriptor) >> 3;
754 desc.Length8 = (u16)(packetLenAligned >> 3);
755 desc.TransactionId = RequestId;
757 sg_init_table(bufferList, 3);
758 sg_set_buf(&bufferList[0], &desc, sizeof(struct vmpacket_descriptor));
759 sg_set_buf(&bufferList[1], Buffer, BufferLen);
760 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
762 ret = RingBufferWrite(&Channel->Outbound, bufferList, 3);
764 /* TODO: We should determine if this is optional */
765 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
766 VmbusChannelSetEvent(Channel);
772 EXPORT_SYMBOL(VmbusChannelSendPacket);
775 * VmbusChannelSendPacketPageBuffer - Send a range of single-page buffer
776 * packets using a GPADL Direct packet type.
778 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel *Channel,
779 struct hv_page_buffer PageBuffers[],
780 u32 PageCount, void *Buffer, u32 BufferLen,
785 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc;
788 u32 packetLenAligned;
789 struct scatterlist bufferList[3];
792 if (PageCount > MAX_PAGE_BUFFER_COUNT)
795 DumpVmbusChannel(Channel);
798 * Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the
799 * largest size we support
801 descSize = sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER) -
802 ((MAX_PAGE_BUFFER_COUNT - PageCount) *
803 sizeof(struct hv_page_buffer));
804 packetLen = descSize + BufferLen;
805 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
807 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
809 /* Setup the descriptor */
810 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
811 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
812 desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
813 desc.Length8 = (u16)(packetLenAligned >> 3);
814 desc.TransactionId = RequestId;
815 desc.RangeCount = PageCount;
817 for (i = 0; i < PageCount; i++) {
818 desc.Range[i].Length = PageBuffers[i].Length;
819 desc.Range[i].Offset = PageBuffers[i].Offset;
820 desc.Range[i].Pfn = PageBuffers[i].Pfn;
823 sg_init_table(bufferList, 3);
824 sg_set_buf(&bufferList[0], &desc, descSize);
825 sg_set_buf(&bufferList[1], Buffer, BufferLen);
826 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
828 ret = RingBufferWrite(&Channel->Outbound, bufferList, 3);
830 /* TODO: We should determine if this is optional */
831 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
832 VmbusChannelSetEvent(Channel);
840 * VmbusChannelSendPacketMultiPageBuffer - Send a multi-page buffer packet
841 * using a GPADL Direct packet type.
843 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel *Channel,
844 struct hv_multipage_buffer *MultiPageBuffer,
845 void *Buffer, u32 BufferLen, u64 RequestId)
848 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc;
851 u32 packetLenAligned;
852 struct scatterlist bufferList[3];
854 u32 PfnCount = NUM_PAGES_SPANNED(MultiPageBuffer->Offset,
855 MultiPageBuffer->Length);
857 DumpVmbusChannel(Channel);
859 DPRINT_DBG(VMBUS, "data buffer - offset %u len %u pfn count %u",
860 MultiPageBuffer->Offset, MultiPageBuffer->Length, PfnCount);
862 if ((PfnCount < 0) || (PfnCount > MAX_MULTIPAGE_BUFFER_COUNT))
866 * Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is
867 * the largest size we support
869 descSize = sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER) -
870 ((MAX_MULTIPAGE_BUFFER_COUNT - PfnCount) *
872 packetLen = descSize + BufferLen;
873 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
875 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
877 /* Setup the descriptor */
878 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
879 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
880 desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
881 desc.Length8 = (u16)(packetLenAligned >> 3);
882 desc.TransactionId = RequestId;
885 desc.Range.Length = MultiPageBuffer->Length;
886 desc.Range.Offset = MultiPageBuffer->Offset;
888 memcpy(desc.Range.PfnArray, MultiPageBuffer->PfnArray,
889 PfnCount * sizeof(u64));
891 sg_init_table(bufferList, 3);
892 sg_set_buf(&bufferList[0], &desc, descSize);
893 sg_set_buf(&bufferList[1], Buffer, BufferLen);
894 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
896 ret = RingBufferWrite(&Channel->Outbound, bufferList, 3);
898 /* TODO: We should determine if this is optional */
899 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
900 VmbusChannelSetEvent(Channel);
909 * VmbusChannelRecvPacket() - Retrieve the user packet on the specified channel
910 * @Channel: Pointer to vmbus_channel structure.
911 * @Buffer: Pointer to the buffer you want to receive the data into.
912 * @BufferLen: Maximum size of what the the buffer will hold
913 * @BufferActualLen: The actual size of the data after it was received
914 * @RequestId: Identifier of the request
916 * Receives directly from the hyper-v vmbus and puts the data it received
917 * into Buffer. This will receive the data unparsed from hyper-v.
919 * Mainly used by Hyper-V drivers.
921 int VmbusChannelRecvPacket(struct vmbus_channel *Channel, void *Buffer,
922 u32 BufferLen, u32 *BufferActualLen, u64 *RequestId)
924 struct vmpacket_descriptor desc;
930 *BufferActualLen = 0;
933 spin_lock_irqsave(&Channel->inbound_lock, flags);
935 ret = RingBufferPeek(&Channel->Inbound, &desc,
936 sizeof(struct vmpacket_descriptor));
938 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
940 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
945 /* VmbusChannelClearEvent(Channel); */
947 packetLen = desc.Length8 << 3;
948 userLen = packetLen - (desc.DataOffset8 << 3);
949 /* ASSERT(userLen > 0); */
951 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d "
952 "flag %d tid %llx pktlen %d datalen %d> ",
953 Channel, Channel->OfferMsg.ChildRelId, desc.Type,
954 desc.Flags, desc.TransactionId, packetLen, userLen);
956 *BufferActualLen = userLen;
958 if (userLen > BufferLen) {
959 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
961 DPRINT_ERR(VMBUS, "buffer too small - got %d needs %d",
968 *RequestId = desc.TransactionId;
970 /* Copy over the packet to the user buffer */
971 ret = RingBufferRead(&Channel->Inbound, Buffer, userLen,
972 (desc.DataOffset8 << 3));
974 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
980 EXPORT_SYMBOL(VmbusChannelRecvPacket);
983 * VmbusChannelRecvPacketRaw - Retrieve the raw packet on the specified channel
985 int VmbusChannelRecvPacketRaw(struct vmbus_channel *Channel, void *Buffer,
986 u32 BufferLen, u32 *BufferActualLen,
989 struct vmpacket_descriptor desc;
995 *BufferActualLen = 0;
998 spin_lock_irqsave(&Channel->inbound_lock, flags);
1000 ret = RingBufferPeek(&Channel->Inbound, &desc,
1001 sizeof(struct vmpacket_descriptor));
1003 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1005 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1010 /* VmbusChannelClearEvent(Channel); */
1012 packetLen = desc.Length8 << 3;
1013 userLen = packetLen - (desc.DataOffset8 << 3);
1015 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d "
1016 "flag %d tid %llx pktlen %d datalen %d> ",
1017 Channel, Channel->OfferMsg.ChildRelId, desc.Type,
1018 desc.Flags, desc.TransactionId, packetLen, userLen);
1020 *BufferActualLen = packetLen;
1022 if (packetLen > BufferLen) {
1023 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1025 DPRINT_ERR(VMBUS, "buffer too small - needed %d bytes but "
1026 "got space for only %d bytes", packetLen, BufferLen);
1031 *RequestId = desc.TransactionId;
1033 /* Copy over the entire packet to the user buffer */
1034 ret = RingBufferRead(&Channel->Inbound, Buffer, packetLen, 0);
1036 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1044 * VmbusChannelOnChannelEvent - Channel event callback
1046 void VmbusChannelOnChannelEvent(struct vmbus_channel *Channel)
1048 DumpVmbusChannel(Channel);
1049 /* ASSERT(Channel->OnChannelCallback); */
1051 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1053 mod_timer(&Channel->poll_timer, jiffies + usecs_to_jiffies(100));
1057 * VmbusChannelOnTimer - Timer event callback
1059 void VmbusChannelOnTimer(unsigned long data)
1061 struct vmbus_channel *channel = (struct vmbus_channel *)data;
1063 if (channel->OnChannelCallback)
1064 channel->OnChannelCallback(channel->ChannelCallbackContext);
1068 * DumpVmbusChannel - Dump vmbus channel info to the console
1070 static void DumpVmbusChannel(struct vmbus_channel *Channel)
1072 DPRINT_DBG(VMBUS, "Channel (%d)", Channel->OfferMsg.ChildRelId);
1073 DumpRingInfo(&Channel->Outbound, "Outbound ");
1074 DumpRingInfo(&Channel->Inbound, "Inbound ");