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;
79 if (Channel->OfferMsg.MonitorAllocated) {
80 /* Each u32 represents 32 channels */
81 set_bit(Channel->OfferMsg.ChildRelId & 31,
82 (unsigned long *) gVmbusConnection.SendInterruptPage +
83 (Channel->OfferMsg.ChildRelId >> 5));
85 monitorPage = gVmbusConnection.MonitorPages;
86 monitorPage++; /* Get the child to parent monitor page */
88 set_bit(Channel->MonitorBit,
89 (unsigned long *)&monitorPage->TriggerGroup
90 [Channel->MonitorGroup].Pending);
93 VmbusSetEvent(Channel->OfferMsg.ChildRelId);
100 static void VmbusChannelClearEvent(struct vmbus_channel *channel)
102 struct hv_monitor_page *monitorPage;
106 if (Channel->OfferMsg.MonitorAllocated) {
107 /* Each u32 represents 32 channels */
108 clear_bit(Channel->OfferMsg.ChildRelId & 31,
109 (unsigned long *)gVmbusConnection.SendInterruptPage +
110 (Channel->OfferMsg.ChildRelId >> 5));
113 (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
114 monitorPage++; /* Get the child to parent monitor page */
116 clear_bit(Channel->MonitorBit,
117 (unsigned long *)&monitorPage->TriggerGroup
118 [Channel->MonitorGroup].Pending);
126 * VmbusChannelGetDebugInfo -Retrieve various channel debug info
128 void VmbusChannelGetDebugInfo(struct vmbus_channel *Channel,
129 struct vmbus_channel_debug_info *DebugInfo)
131 struct hv_monitor_page *monitorPage;
132 u8 monitorGroup = (u8)Channel->OfferMsg.MonitorId / 32;
133 u8 monitorOffset = (u8)Channel->OfferMsg.MonitorId % 32;
134 /* u32 monitorBit = 1 << monitorOffset; */
136 DebugInfo->RelId = Channel->OfferMsg.ChildRelId;
137 DebugInfo->State = Channel->State;
138 memcpy(&DebugInfo->InterfaceType,
139 &Channel->OfferMsg.Offer.InterfaceType, sizeof(struct hv_guid));
140 memcpy(&DebugInfo->InterfaceInstance,
141 &Channel->OfferMsg.Offer.InterfaceInstance,
142 sizeof(struct hv_guid));
144 monitorPage = (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
146 DebugInfo->MonitorId = Channel->OfferMsg.MonitorId;
148 DebugInfo->ServerMonitorPending =
149 monitorPage->TriggerGroup[monitorGroup].Pending;
150 DebugInfo->ServerMonitorLatency =
151 monitorPage->Latency[monitorGroup][monitorOffset];
152 DebugInfo->ServerMonitorConnectionId =
153 monitorPage->Parameter[monitorGroup]
154 [monitorOffset].ConnectionId.u.Id;
158 DebugInfo->ClientMonitorPending =
159 monitorPage->TriggerGroup[monitorGroup].Pending;
160 DebugInfo->ClientMonitorLatency =
161 monitorPage->Latency[monitorGroup][monitorOffset];
162 DebugInfo->ClientMonitorConnectionId =
163 monitorPage->Parameter[monitorGroup]
164 [monitorOffset].ConnectionId.u.Id;
166 RingBufferGetDebugInfo(&Channel->Inbound, &DebugInfo->Inbound);
167 RingBufferGetDebugInfo(&Channel->Outbound, &DebugInfo->Outbound);
171 * VmbusChannelOpen - Open the specified channel.
173 int VmbusChannelOpen(struct vmbus_channel *NewChannel, u32 SendRingBufferSize,
174 u32 RecvRingBufferSize, void *UserData, u32 UserDataLen,
175 void (*OnChannelCallback)(void *context), void *Context)
177 struct vmbus_channel_open_channel *openMsg;
178 struct vmbus_channel_msginfo *openInfo = NULL;
185 /* Aligned to page size */
186 /* ASSERT(!(SendRingBufferSize & (PAGE_SIZE - 1))); */
187 /* ASSERT(!(RecvRingBufferSize & (PAGE_SIZE - 1))); */
189 NewChannel->OnChannelCallback = OnChannelCallback;
190 NewChannel->ChannelCallbackContext = Context;
192 /* Allocate the ring buffer */
193 out = osd_PageAlloc((SendRingBufferSize + RecvRingBufferSize)
198 /* ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0); */
200 in = (void *)((unsigned long)out + SendRingBufferSize);
202 NewChannel->RingBufferPages = out;
203 NewChannel->RingBufferPageCount = (SendRingBufferSize +
204 RecvRingBufferSize) >> PAGE_SHIFT;
206 ret = RingBufferInit(&NewChannel->Outbound, out, SendRingBufferSize);
212 ret = RingBufferInit(&NewChannel->Inbound, in, RecvRingBufferSize);
219 /* Establish the gpadl for the ring buffer */
220 DPRINT_DBG(VMBUS, "Establishing ring buffer's gpadl for channel %p...",
223 NewChannel->RingBufferGpadlHandle = 0;
225 ret = VmbusChannelEstablishGpadl(NewChannel,
226 NewChannel->Outbound.RingBuffer,
229 &NewChannel->RingBufferGpadlHandle);
236 DPRINT_DBG(VMBUS, "channel %p <relid %d gpadl 0x%x send ring %p "
237 "size %d recv ring %p size %d, downstreamoffset %d>",
238 NewChannel, NewChannel->OfferMsg.ChildRelId,
239 NewChannel->RingBufferGpadlHandle,
240 NewChannel->Outbound.RingBuffer,
241 NewChannel->Outbound.RingSize,
242 NewChannel->Inbound.RingBuffer,
243 NewChannel->Inbound.RingSize,
246 /* Create and init the channel open message */
247 openInfo = kmalloc(sizeof(*openInfo) +
248 sizeof(struct vmbus_channel_open_channel),
255 openInfo->WaitEvent = osd_WaitEventCreate();
256 if (!openInfo->WaitEvent) {
261 openMsg = (struct vmbus_channel_open_channel *)openInfo->Msg;
262 openMsg->Header.MessageType = ChannelMessageOpenChannel;
263 openMsg->OpenId = NewChannel->OfferMsg.ChildRelId; /* FIXME */
264 openMsg->ChildRelId = NewChannel->OfferMsg.ChildRelId;
265 openMsg->RingBufferGpadlHandle = NewChannel->RingBufferGpadlHandle;
266 openMsg->DownstreamRingBufferPageOffset = SendRingBufferSize >>
268 openMsg->ServerContextAreaGpadlHandle = 0; /* TODO */
270 if (UserDataLen > MAX_USER_DEFINED_BYTES) {
276 memcpy(openMsg->UserData, UserData, UserDataLen);
278 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
279 list_add_tail(&openInfo->MsgListEntry,
280 &gVmbusConnection.ChannelMsgList);
281 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
283 DPRINT_DBG(VMBUS, "Sending channel open msg...");
285 ret = VmbusPostMessage(openMsg,
286 sizeof(struct vmbus_channel_open_channel));
288 DPRINT_ERR(VMBUS, "unable to open channel - %d", ret);
292 /* FIXME: Need to time-out here */
293 osd_WaitEventWait(openInfo->WaitEvent);
295 if (openInfo->Response.OpenResult.Status == 0)
296 DPRINT_INFO(VMBUS, "channel <%p> open success!!", NewChannel);
298 DPRINT_INFO(VMBUS, "channel <%p> open failed - %d!!",
299 NewChannel, openInfo->Response.OpenResult.Status);
302 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
303 list_del(&openInfo->MsgListEntry);
304 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
306 kfree(openInfo->WaitEvent);
314 RingBufferCleanup(&NewChannel->Outbound);
315 RingBufferCleanup(&NewChannel->Inbound);
316 osd_PageFree(out, (SendRingBufferSize + RecvRingBufferSize)
323 * DumpGpadlBody - Dump the gpadl body message to the console for
324 * debugging purposes.
326 static void DumpGpadlBody(struct vmbus_channel_gpadl_body *Gpadl, u32 Len)
331 pfnCount = (Len - sizeof(struct vmbus_channel_gpadl_body)) /
333 DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", Len, pfnCount);
335 for (i = 0; i < pfnCount; i++)
336 DPRINT_DBG(VMBUS, "gpadl body - %d) pfn %llu",
341 * DumpGpadlHeader - Dump the gpadl header message to the console for
342 * debugging purposes.
344 static void DumpGpadlHeader(struct vmbus_channel_gpadl_header *Gpadl)
350 "gpadl header - relid %d, range count %d, range buflen %d",
351 Gpadl->ChildRelId, Gpadl->RangeCount, Gpadl->RangeBufLen);
352 for (i = 0; i < Gpadl->RangeCount; i++) {
353 pageCount = Gpadl->Range[i].ByteCount >> PAGE_SHIFT;
354 pageCount = (pageCount > 26) ? 26 : pageCount;
356 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d "
357 "page count %d", i, Gpadl->Range[i].ByteCount,
358 Gpadl->Range[i].ByteOffset, pageCount);
360 for (j = 0; j < pageCount; j++)
361 DPRINT_DBG(VMBUS, "%d) pfn %llu", j,
362 Gpadl->Range[i].PfnArray[j]);
367 * VmbusChannelCreateGpadlHeader - Creates a gpadl for the specified buffer
369 static int VmbusChannelCreateGpadlHeader(void *Kbuffer, u32 Size,
370 struct vmbus_channel_msginfo **MsgInfo,
375 unsigned long long pfn;
376 struct vmbus_channel_gpadl_header *gpaHeader;
377 struct vmbus_channel_gpadl_body *gpadlBody;
378 struct vmbus_channel_msginfo *msgHeader;
379 struct vmbus_channel_msginfo *msgBody = NULL;
382 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
384 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
385 /* ASSERT((Size & (PAGE_SIZE-1)) == 0); */
387 pageCount = Size >> PAGE_SHIFT;
388 pfn = virt_to_phys(Kbuffer) >> PAGE_SHIFT;
390 /* do we need a gpadl body msg */
391 pfnSize = MAX_SIZE_CHANNEL_MESSAGE -
392 sizeof(struct vmbus_channel_gpadl_header) -
393 sizeof(struct gpa_range);
394 pfnCount = pfnSize / sizeof(u64);
396 if (pageCount > pfnCount) {
397 /* we need a gpadl body */
398 /* fill in the header */
399 msgSize = sizeof(struct vmbus_channel_msginfo) +
400 sizeof(struct vmbus_channel_gpadl_header) +
401 sizeof(struct gpa_range) + pfnCount * sizeof(u64);
402 msgHeader = kzalloc(msgSize, GFP_KERNEL);
406 INIT_LIST_HEAD(&msgHeader->SubMsgList);
407 msgHeader->MessageSize = msgSize;
409 gpaHeader = (struct vmbus_channel_gpadl_header *)msgHeader->Msg;
410 gpaHeader->RangeCount = 1;
411 gpaHeader->RangeBufLen = sizeof(struct gpa_range) +
412 pageCount * sizeof(u64);
413 gpaHeader->Range[0].ByteOffset = 0;
414 gpaHeader->Range[0].ByteCount = Size;
415 for (i = 0; i < pfnCount; i++)
416 gpaHeader->Range[0].PfnArray[i] = pfn+i;
417 *MsgInfo = msgHeader;
421 pfnLeft = pageCount - pfnCount;
423 /* how many pfns can we fit */
424 pfnSize = MAX_SIZE_CHANNEL_MESSAGE -
425 sizeof(struct vmbus_channel_gpadl_body);
426 pfnCount = pfnSize / sizeof(u64);
428 /* fill in the body */
430 if (pfnLeft > pfnCount)
435 msgSize = sizeof(struct vmbus_channel_msginfo) +
436 sizeof(struct vmbus_channel_gpadl_body) +
437 pfnCurr * sizeof(u64);
438 msgBody = kzalloc(msgSize, GFP_KERNEL);
439 /* FIXME: we probably need to more if this fails */
442 msgBody->MessageSize = msgSize;
445 (struct vmbus_channel_gpadl_body *)msgBody->Msg;
449 * Gpadl is u32 and we are using a pointer which could
452 /* gpadlBody->Gpadl = kbuffer; */
453 for (i = 0; i < pfnCurr; i++)
454 gpadlBody->Pfn[i] = pfn + pfnSum + i;
456 /* add to msg header */
457 list_add_tail(&msgBody->MsgListEntry,
458 &msgHeader->SubMsgList);
463 /* everything fits in a header */
464 msgSize = sizeof(struct vmbus_channel_msginfo) +
465 sizeof(struct vmbus_channel_gpadl_header) +
466 sizeof(struct gpa_range) + pageCount * sizeof(u64);
467 msgHeader = kzalloc(msgSize, GFP_KERNEL);
468 if (msgHeader == NULL)
470 msgHeader->MessageSize = msgSize;
472 gpaHeader = (struct vmbus_channel_gpadl_header *)msgHeader->Msg;
473 gpaHeader->RangeCount = 1;
474 gpaHeader->RangeBufLen = sizeof(struct gpa_range) +
475 pageCount * sizeof(u64);
476 gpaHeader->Range[0].ByteOffset = 0;
477 gpaHeader->Range[0].ByteCount = Size;
478 for (i = 0; i < pageCount; i++)
479 gpaHeader->Range[0].PfnArray[i] = pfn+i;
481 *MsgInfo = msgHeader;
493 * VmbusChannelEstablishGpadl - Estabish a GPADL for the specified buffer
495 * @Channel: a channel
496 * @Kbuffer: from kmalloc
497 * @Size: page-size multiple
498 * @GpadlHandle: some funky thing
500 int VmbusChannelEstablishGpadl(struct vmbus_channel *Channel, void *Kbuffer,
501 u32 Size, u32 *GpadlHandle)
503 struct vmbus_channel_gpadl_header *gpadlMsg;
504 struct vmbus_channel_gpadl_body *gpadlBody;
505 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
506 struct vmbus_channel_msginfo *msgInfo = NULL;
507 struct vmbus_channel_msginfo *subMsgInfo;
509 struct list_head *curr;
516 nextGpadlHandle = atomic_read(&gVmbusConnection.NextGpadlHandle);
517 atomic_inc(&gVmbusConnection.NextGpadlHandle);
519 ret = VmbusChannelCreateGpadlHeader(Kbuffer, Size, &msgInfo, &msgCount);
523 msgInfo->WaitEvent = osd_WaitEventCreate();
524 if (!msgInfo->WaitEvent) {
529 gpadlMsg = (struct vmbus_channel_gpadl_header *)msgInfo->Msg;
530 gpadlMsg->Header.MessageType = ChannelMessageGpadlHeader;
531 gpadlMsg->ChildRelId = Channel->OfferMsg.ChildRelId;
532 gpadlMsg->Gpadl = nextGpadlHandle;
534 DumpGpadlHeader(gpadlMsg);
536 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
537 list_add_tail(&msgInfo->MsgListEntry,
538 &gVmbusConnection.ChannelMsgList);
540 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
541 DPRINT_DBG(VMBUS, "buffer %p, size %d msg cnt %d",
542 Kbuffer, Size, msgCount);
544 DPRINT_DBG(VMBUS, "Sending GPADL Header - len %zd",
545 msgInfo->MessageSize - sizeof(*msgInfo));
547 ret = VmbusPostMessage(gpadlMsg, msgInfo->MessageSize -
550 DPRINT_ERR(VMBUS, "Unable to open channel - %d", ret);
555 list_for_each(curr, &msgInfo->SubMsgList) {
557 /* FIXME: should this use list_entry() instead ? */
558 subMsgInfo = (struct vmbus_channel_msginfo *)curr;
560 (struct vmbus_channel_gpadl_body *)subMsgInfo->Msg;
562 gpadlBody->Header.MessageType = ChannelMessageGpadlBody;
563 gpadlBody->Gpadl = nextGpadlHandle;
565 DPRINT_DBG(VMBUS, "Sending GPADL Body - len %zd",
566 subMsgInfo->MessageSize -
567 sizeof(*subMsgInfo));
569 DumpGpadlBody(gpadlBody, subMsgInfo->MessageSize -
570 sizeof(*subMsgInfo));
571 ret = VmbusPostMessage(gpadlBody,
572 subMsgInfo->MessageSize -
573 sizeof(*subMsgInfo));
579 osd_WaitEventWait(msgInfo->WaitEvent);
581 /* At this point, we received the gpadl created msg */
582 DPRINT_DBG(VMBUS, "Received GPADL created "
583 "(relid %d, status %d handle %x)",
584 Channel->OfferMsg.ChildRelId,
585 msgInfo->Response.GpadlCreated.CreationStatus,
588 *GpadlHandle = gpadlMsg->Gpadl;
591 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
592 list_del(&msgInfo->MsgListEntry);
593 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
595 kfree(msgInfo->WaitEvent);
604 * VmbusChannelTeardownGpadl -Teardown the specified GPADL handle
606 int VmbusChannelTeardownGpadl(struct vmbus_channel *Channel, u32 GpadlHandle)
608 struct vmbus_channel_gpadl_teardown *msg;
609 struct vmbus_channel_msginfo *info;
615 /* ASSERT(GpadlHandle != 0); */
617 info = kmalloc(sizeof(*info) +
618 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
622 info->WaitEvent = osd_WaitEventCreate();
623 if (!info->WaitEvent) {
628 msg = (struct vmbus_channel_gpadl_teardown *)info->Msg;
630 msg->Header.MessageType = ChannelMessageGpadlTeardown;
631 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
632 msg->Gpadl = GpadlHandle;
634 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
635 list_add_tail(&info->MsgListEntry,
636 &gVmbusConnection.ChannelMsgList);
637 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
639 ret = VmbusPostMessage(msg,
640 sizeof(struct vmbus_channel_gpadl_teardown));
646 osd_WaitEventWait(info->WaitEvent);
648 /* Received a torndown response */
649 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
650 list_del(&info->MsgListEntry);
651 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
653 kfree(info->WaitEvent);
662 * VmbusChannelClose - Close the specified channel
664 void VmbusChannelClose(struct vmbus_channel *Channel)
666 struct vmbus_channel_close_channel *msg;
667 struct vmbus_channel_msginfo *info;
673 /* Stop callback and cancel the timer asap */
674 Channel->OnChannelCallback = NULL;
675 del_timer_sync(&Channel->poll_timer);
677 /* Send a closing message */
678 info = kmalloc(sizeof(*info) +
679 sizeof(struct vmbus_channel_close_channel), GFP_KERNEL);
680 /* FIXME: can't do anything other than return here because the
681 * function is void */
685 /* info->waitEvent = osd_WaitEventCreate(); */
687 msg = (struct vmbus_channel_close_channel *)info->Msg;
688 msg->Header.MessageType = ChannelMessageCloseChannel;
689 msg->ChildRelId = Channel->OfferMsg.ChildRelId;
691 ret = VmbusPostMessage(msg, sizeof(struct vmbus_channel_close_channel));
697 /* Tear down the gpadl for the channel's ring buffer */
698 if (Channel->RingBufferGpadlHandle)
699 VmbusChannelTeardownGpadl(Channel,
700 Channel->RingBufferGpadlHandle);
702 /* TODO: Send a msg to release the childRelId */
704 /* Cleanup the ring buffers for this channel */
705 RingBufferCleanup(&Channel->Outbound);
706 RingBufferCleanup(&Channel->Inbound);
708 osd_PageFree(Channel->RingBufferPages, Channel->RingBufferPageCount);
713 * If we are closing the channel during an error path in
714 * opening the channel, don't free the channel since the
715 * caller will free the channel
718 if (Channel->State == CHANNEL_OPEN_STATE) {
719 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
720 list_del(&Channel->ListEntry);
721 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
723 FreeVmbusChannel(Channel);
730 * VmbusChannelSendPacket() - Send the specified buffer on the given channel
731 * @Channel: Pointer to vmbus_channel structure.
732 * @Buffer: Pointer to the buffer you want to receive the data into.
733 * @BufferLen: Maximum size of what the the buffer will hold
734 * @RequestId: Identifier of the request
735 * @vmbus_packet_type: Type of packet that is being send e.g. negotiate, time
738 * Sends data in @Buffer directly to hyper-v via the vmbus
739 * This will send the data unparsed to hyper-v.
741 * Mainly used by Hyper-V drivers.
743 int VmbusChannelSendPacket(struct vmbus_channel *Channel, const void *Buffer,
744 u32 BufferLen, u64 RequestId,
745 enum vmbus_packet_type Type, u32 Flags)
747 struct vmpacket_descriptor desc;
748 u32 packetLen = sizeof(struct vmpacket_descriptor) + BufferLen;
749 u32 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
750 struct scatterlist bufferList[3];
755 DPRINT_DBG(VMBUS, "channel %p buffer %p len %d",
756 Channel, Buffer, BufferLen);
758 DumpVmbusChannel(Channel);
760 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
762 /* Setup the descriptor */
763 desc.Type = Type; /* VmbusPacketTypeDataInBand; */
764 desc.Flags = Flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
765 /* in 8-bytes granularity */
766 desc.DataOffset8 = sizeof(struct vmpacket_descriptor) >> 3;
767 desc.Length8 = (u16)(packetLenAligned >> 3);
768 desc.TransactionId = RequestId;
770 sg_init_table(bufferList, 3);
771 sg_set_buf(&bufferList[0], &desc, sizeof(struct vmpacket_descriptor));
772 sg_set_buf(&bufferList[1], Buffer, BufferLen);
773 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
775 ret = RingBufferWrite(&Channel->Outbound, bufferList, 3);
777 /* TODO: We should determine if this is optional */
778 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
779 VmbusChannelSetEvent(Channel);
785 EXPORT_SYMBOL(VmbusChannelSendPacket);
788 * VmbusChannelSendPacketPageBuffer - Send a range of single-page buffer
789 * packets using a GPADL Direct packet type.
791 int VmbusChannelSendPacketPageBuffer(struct vmbus_channel *Channel,
792 struct hv_page_buffer PageBuffers[],
793 u32 PageCount, void *Buffer, u32 BufferLen,
798 struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER desc;
801 u32 packetLenAligned;
802 struct scatterlist bufferList[3];
807 if (PageCount > MAX_PAGE_BUFFER_COUNT)
810 DumpVmbusChannel(Channel);
813 * Adjust the size down since VMBUS_CHANNEL_PACKET_PAGE_BUFFER is the
814 * largest size we support
816 descSize = sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER) -
817 ((MAX_PAGE_BUFFER_COUNT - PageCount) *
818 sizeof(struct hv_page_buffer));
819 packetLen = descSize + BufferLen;
820 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
822 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
824 /* Setup the descriptor */
825 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
826 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
827 desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
828 desc.Length8 = (u16)(packetLenAligned >> 3);
829 desc.TransactionId = RequestId;
830 desc.RangeCount = PageCount;
832 for (i = 0; i < PageCount; i++) {
833 desc.Range[i].Length = PageBuffers[i].Length;
834 desc.Range[i].Offset = PageBuffers[i].Offset;
835 desc.Range[i].Pfn = PageBuffers[i].Pfn;
838 sg_init_table(bufferList, 3);
839 sg_set_buf(&bufferList[0], &desc, descSize);
840 sg_set_buf(&bufferList[1], Buffer, BufferLen);
841 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
843 ret = RingBufferWrite(&Channel->Outbound, bufferList, 3);
845 /* TODO: We should determine if this is optional */
846 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
847 VmbusChannelSetEvent(Channel);
855 * VmbusChannelSendPacketMultiPageBuffer - Send a multi-page buffer packet
856 * using a GPADL Direct packet type.
858 int VmbusChannelSendPacketMultiPageBuffer(struct vmbus_channel *Channel,
859 struct hv_multipage_buffer *MultiPageBuffer,
860 void *Buffer, u32 BufferLen, u64 RequestId)
863 struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER desc;
866 u32 packetLenAligned;
867 struct scatterlist bufferList[3];
869 u32 PfnCount = NUM_PAGES_SPANNED(MultiPageBuffer->Offset,
870 MultiPageBuffer->Length);
874 DumpVmbusChannel(Channel);
876 DPRINT_DBG(VMBUS, "data buffer - offset %u len %u pfn count %u",
877 MultiPageBuffer->Offset, MultiPageBuffer->Length, PfnCount);
879 if ((PfnCount < 0) || (PfnCount > MAX_MULTIPAGE_BUFFER_COUNT))
883 * Adjust the size down since VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER is
884 * the largest size we support
886 descSize = sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER) -
887 ((MAX_MULTIPAGE_BUFFER_COUNT - PfnCount) *
889 packetLen = descSize + BufferLen;
890 packetLenAligned = ALIGN_UP(packetLen, sizeof(u64));
892 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
894 /* Setup the descriptor */
895 desc.Type = VmbusPacketTypeDataUsingGpaDirect;
896 desc.Flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
897 desc.DataOffset8 = descSize >> 3; /* in 8-bytes grandularity */
898 desc.Length8 = (u16)(packetLenAligned >> 3);
899 desc.TransactionId = RequestId;
902 desc.Range.Length = MultiPageBuffer->Length;
903 desc.Range.Offset = MultiPageBuffer->Offset;
905 memcpy(desc.Range.PfnArray, MultiPageBuffer->PfnArray,
906 PfnCount * sizeof(u64));
908 sg_init_table(bufferList, 3);
909 sg_set_buf(&bufferList[0], &desc, descSize);
910 sg_set_buf(&bufferList[1], Buffer, BufferLen);
911 sg_set_buf(&bufferList[2], &alignedData, packetLenAligned - packetLen);
913 ret = RingBufferWrite(&Channel->Outbound, bufferList, 3);
915 /* TODO: We should determine if this is optional */
916 if (ret == 0 && !GetRingBufferInterruptMask(&Channel->Outbound))
917 VmbusChannelSetEvent(Channel);
926 * VmbusChannelRecvPacket() - Retrieve the user packet on the specified channel
927 * @Channel: Pointer to vmbus_channel structure.
928 * @Buffer: Pointer to the buffer you want to receive the data into.
929 * @BufferLen: Maximum size of what the the buffer will hold
930 * @BufferActualLen: The actual size of the data after it was received
931 * @RequestId: Identifier of the request
933 * Receives directly from the hyper-v vmbus and puts the data it received
934 * into Buffer. This will receive the data unparsed from hyper-v.
936 * Mainly used by Hyper-V drivers.
938 int VmbusChannelRecvPacket(struct vmbus_channel *Channel, void *Buffer,
939 u32 BufferLen, u32 *BufferActualLen, u64 *RequestId)
941 struct vmpacket_descriptor desc;
949 *BufferActualLen = 0;
952 spin_lock_irqsave(&Channel->inbound_lock, flags);
954 ret = RingBufferPeek(&Channel->Inbound, &desc,
955 sizeof(struct vmpacket_descriptor));
957 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
959 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
964 /* VmbusChannelClearEvent(Channel); */
966 packetLen = desc.Length8 << 3;
967 userLen = packetLen - (desc.DataOffset8 << 3);
968 /* ASSERT(userLen > 0); */
970 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d "
971 "flag %d tid %llx pktlen %d datalen %d> ",
972 Channel, Channel->OfferMsg.ChildRelId, desc.Type,
973 desc.Flags, desc.TransactionId, packetLen, userLen);
975 *BufferActualLen = userLen;
977 if (userLen > BufferLen) {
978 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
980 DPRINT_ERR(VMBUS, "buffer too small - got %d needs %d",
987 *RequestId = desc.TransactionId;
989 /* Copy over the packet to the user buffer */
990 ret = RingBufferRead(&Channel->Inbound, Buffer, userLen,
991 (desc.DataOffset8 << 3));
993 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
999 EXPORT_SYMBOL(VmbusChannelRecvPacket);
1002 * VmbusChannelRecvPacketRaw - Retrieve the raw packet on the specified channel
1004 int VmbusChannelRecvPacketRaw(struct vmbus_channel *Channel, void *Buffer,
1005 u32 BufferLen, u32 *BufferActualLen,
1008 struct vmpacket_descriptor desc;
1012 unsigned long flags;
1014 DPRINT_ENTER(VMBUS);
1016 *BufferActualLen = 0;
1019 spin_lock_irqsave(&Channel->inbound_lock, flags);
1021 ret = RingBufferPeek(&Channel->Inbound, &desc,
1022 sizeof(struct vmpacket_descriptor));
1024 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1026 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
1031 /* VmbusChannelClearEvent(Channel); */
1033 packetLen = desc.Length8 << 3;
1034 userLen = packetLen - (desc.DataOffset8 << 3);
1036 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d "
1037 "flag %d tid %llx pktlen %d datalen %d> ",
1038 Channel, Channel->OfferMsg.ChildRelId, desc.Type,
1039 desc.Flags, desc.TransactionId, packetLen, userLen);
1041 *BufferActualLen = packetLen;
1043 if (packetLen > BufferLen) {
1044 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1046 DPRINT_ERR(VMBUS, "buffer too small - needed %d bytes but "
1047 "got space for only %d bytes", packetLen, BufferLen);
1052 *RequestId = desc.TransactionId;
1054 /* Copy over the entire packet to the user buffer */
1055 ret = RingBufferRead(&Channel->Inbound, Buffer, packetLen, 0);
1057 spin_unlock_irqrestore(&Channel->inbound_lock, flags);
1065 * VmbusChannelOnChannelEvent - Channel event callback
1067 void VmbusChannelOnChannelEvent(struct vmbus_channel *Channel)
1069 DumpVmbusChannel(Channel);
1070 /* ASSERT(Channel->OnChannelCallback); */
1072 Channel->OnChannelCallback(Channel->ChannelCallbackContext);
1074 mod_timer(&Channel->poll_timer, jiffies + usecs_to_jiffies(100));
1078 * VmbusChannelOnTimer - Timer event callback
1080 void VmbusChannelOnTimer(unsigned long data)
1082 struct vmbus_channel *channel = (struct vmbus_channel *)data;
1084 if (channel->OnChannelCallback)
1085 channel->OnChannelCallback(channel->ChannelCallbackContext);
1089 * DumpVmbusChannel - Dump vmbus channel info to the console
1091 static void DumpVmbusChannel(struct vmbus_channel *Channel)
1093 DPRINT_DBG(VMBUS, "Channel (%d)", Channel->OfferMsg.ChildRelId);
1094 DumpRingInfo(&Channel->Outbound, "Outbound ");
1095 DumpRingInfo(&Channel->Inbound, "Inbound ");