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 create_gpadl_header(
31 void *kbuffer, /* must be phys and virt contiguous */
32 u32 size, /* page-size multiple */
33 struct vmbus_channel_msginfo **msginfo,
35 static void dump_vmbus_channel(struct vmbus_channel *channel);
36 static void vmbus_setevent(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 * vmbus_setevent- Trigger an event notification on the specified
73 static void vmbus_setevent(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);
96 static void VmbusChannelClearEvent(struct vmbus_channel *channel)
98 struct hv_monitor_page *monitorPage;
100 if (Channel->OfferMsg.MonitorAllocated) {
101 /* Each u32 represents 32 channels */
102 clear_bit(Channel->OfferMsg.ChildRelId & 31,
103 (unsigned long *)gVmbusConnection.SendInterruptPage +
104 (Channel->OfferMsg.ChildRelId >> 5));
107 (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
108 monitorPage++; /* Get the child to parent monitor page */
110 clear_bit(Channel->MonitorBit,
111 (unsigned long *)&monitorPage->TriggerGroup
112 [Channel->MonitorGroup].Pending);
118 * vmbus_get_debug_info -Retrieve various channel debug info
120 void vmbus_get_debug_info(struct vmbus_channel *channel,
121 struct vmbus_channel_debug_info *debuginfo)
123 struct hv_monitor_page *monitorpage;
124 u8 monitor_group = (u8)channel->OfferMsg.MonitorId / 32;
125 u8 monitor_offset = (u8)channel->OfferMsg.MonitorId % 32;
126 /* u32 monitorBit = 1 << monitorOffset; */
128 debuginfo->RelId = channel->OfferMsg.ChildRelId;
129 debuginfo->State = channel->State;
130 memcpy(&debuginfo->InterfaceType,
131 &channel->OfferMsg.Offer.InterfaceType, sizeof(struct hv_guid));
132 memcpy(&debuginfo->InterfaceInstance,
133 &channel->OfferMsg.Offer.InterfaceInstance,
134 sizeof(struct hv_guid));
136 monitorpage = (struct hv_monitor_page *)gVmbusConnection.MonitorPages;
138 debuginfo->MonitorId = channel->OfferMsg.MonitorId;
140 debuginfo->ServerMonitorPending =
141 monitorpage->TriggerGroup[monitor_group].Pending;
142 debuginfo->ServerMonitorLatency =
143 monitorpage->Latency[monitor_group][monitor_offset];
144 debuginfo->ServerMonitorConnectionId =
145 monitorpage->Parameter[monitor_group]
146 [monitor_offset].ConnectionId.u.Id;
150 debuginfo->ClientMonitorPending =
151 monitorpage->TriggerGroup[monitor_group].Pending;
152 debuginfo->ClientMonitorLatency =
153 monitorpage->Latency[monitor_group][monitor_offset];
154 debuginfo->ClientMonitorConnectionId =
155 monitorpage->Parameter[monitor_group]
156 [monitor_offset].ConnectionId.u.Id;
158 RingBufferGetDebugInfo(&channel->Inbound, &debuginfo->Inbound);
159 RingBufferGetDebugInfo(&channel->Outbound, &debuginfo->Outbound);
163 * vmbus_open - Open the specified channel.
165 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
166 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
167 void (*onchannelcallback)(void *context), void *context)
169 struct vmbus_channel_open_channel *openMsg;
170 struct vmbus_channel_msginfo *openInfo = NULL;
175 /* Aligned to page size */
176 /* ASSERT(!(SendRingBufferSize & (PAGE_SIZE - 1))); */
177 /* ASSERT(!(RecvRingBufferSize & (PAGE_SIZE - 1))); */
179 newchannel->OnChannelCallback = onchannelcallback;
180 newchannel->ChannelCallbackContext = context;
182 /* Allocate the ring buffer */
183 out = osd_PageAlloc((send_ringbuffer_size + recv_ringbuffer_size)
188 /* ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0); */
190 in = (void *)((unsigned long)out + send_ringbuffer_size);
192 newchannel->RingBufferPages = out;
193 newchannel->RingBufferPageCount = (send_ringbuffer_size +
194 recv_ringbuffer_size) >> PAGE_SHIFT;
196 ret = RingBufferInit(&newchannel->Outbound, out, send_ringbuffer_size);
202 ret = RingBufferInit(&newchannel->Inbound, in, recv_ringbuffer_size);
209 /* Establish the gpadl for the ring buffer */
210 DPRINT_DBG(VMBUS, "Establishing ring buffer's gpadl for channel %p...",
213 newchannel->RingBufferGpadlHandle = 0;
215 ret = vmbus_establish_gpadl(newchannel,
216 newchannel->Outbound.RingBuffer,
217 send_ringbuffer_size +
218 recv_ringbuffer_size,
219 &newchannel->RingBufferGpadlHandle);
226 DPRINT_DBG(VMBUS, "channel %p <relid %d gpadl 0x%x send ring %p "
227 "size %d recv ring %p size %d, downstreamoffset %d>",
228 newchannel, newchannel->OfferMsg.ChildRelId,
229 newchannel->RingBufferGpadlHandle,
230 newchannel->Outbound.RingBuffer,
231 newchannel->Outbound.RingSize,
232 newchannel->Inbound.RingBuffer,
233 newchannel->Inbound.RingSize,
234 send_ringbuffer_size);
236 /* Create and init the channel open message */
237 openInfo = kmalloc(sizeof(*openInfo) +
238 sizeof(struct vmbus_channel_open_channel),
245 openInfo->WaitEvent = osd_WaitEventCreate();
246 if (!openInfo->WaitEvent) {
251 openMsg = (struct vmbus_channel_open_channel *)openInfo->Msg;
252 openMsg->Header.MessageType = ChannelMessageOpenChannel;
253 openMsg->OpenId = newchannel->OfferMsg.ChildRelId; /* FIXME */
254 openMsg->ChildRelId = newchannel->OfferMsg.ChildRelId;
255 openMsg->RingBufferGpadlHandle = newchannel->RingBufferGpadlHandle;
256 openMsg->DownstreamRingBufferPageOffset = send_ringbuffer_size >>
258 openMsg->ServerContextAreaGpadlHandle = 0; /* TODO */
260 if (userdatalen > MAX_USER_DEFINED_BYTES) {
266 memcpy(openMsg->UserData, userdata, userdatalen);
268 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
269 list_add_tail(&openInfo->MsgListEntry,
270 &gVmbusConnection.ChannelMsgList);
271 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
273 DPRINT_DBG(VMBUS, "Sending channel open msg...");
275 ret = VmbusPostMessage(openMsg,
276 sizeof(struct vmbus_channel_open_channel));
278 DPRINT_ERR(VMBUS, "unable to open channel - %d", ret);
282 /* FIXME: Need to time-out here */
283 osd_WaitEventWait(openInfo->WaitEvent);
285 if (openInfo->Response.OpenResult.Status == 0)
286 DPRINT_INFO(VMBUS, "channel <%p> open success!!", newchannel);
288 DPRINT_INFO(VMBUS, "channel <%p> open failed - %d!!",
289 newchannel, openInfo->Response.OpenResult.Status);
292 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
293 list_del(&openInfo->MsgListEntry);
294 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
296 kfree(openInfo->WaitEvent);
301 RingBufferCleanup(&newchannel->Outbound);
302 RingBufferCleanup(&newchannel->Inbound);
303 osd_PageFree(out, (send_ringbuffer_size + recv_ringbuffer_size)
310 * dump_gpadl_body - Dump the gpadl body message to the console for
311 * debugging purposes.
313 static void dump_gpadl_body(struct vmbus_channel_gpadl_body *gpadl, u32 len)
318 pfncount = (len - sizeof(struct vmbus_channel_gpadl_body)) /
320 DPRINT_DBG(VMBUS, "gpadl body - len %d pfn count %d", len, pfncount);
322 for (i = 0; i < pfncount; i++)
323 DPRINT_DBG(VMBUS, "gpadl body - %d) pfn %llu",
328 * dump_gpadl_header - Dump the gpadl header message to the console for
329 * debugging purposes.
331 static void dump_gpadl_header(struct vmbus_channel_gpadl_header *gpadl)
337 "gpadl header - relid %d, range count %d, range buflen %d",
338 gpadl->ChildRelId, gpadl->RangeCount, gpadl->RangeBufLen);
339 for (i = 0; i < gpadl->RangeCount; i++) {
340 pagecount = gpadl->Range[i].ByteCount >> PAGE_SHIFT;
341 pagecount = (pagecount > 26) ? 26 : pagecount;
343 DPRINT_DBG(VMBUS, "gpadl range %d - len %d offset %d "
344 "page count %d", i, gpadl->Range[i].ByteCount,
345 gpadl->Range[i].ByteOffset, pagecount);
347 for (j = 0; j < pagecount; j++)
348 DPRINT_DBG(VMBUS, "%d) pfn %llu", j,
349 gpadl->Range[i].PfnArray[j]);
354 * create_gpadl_header - Creates a gpadl for the specified buffer
356 static int create_gpadl_header(void *kbuffer, u32 size,
357 struct vmbus_channel_msginfo **msginfo,
362 unsigned long long pfn;
363 struct vmbus_channel_gpadl_header *gpadl_header;
364 struct vmbus_channel_gpadl_body *gpadl_body;
365 struct vmbus_channel_msginfo *msgheader;
366 struct vmbus_channel_msginfo *msgbody = NULL;
369 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
371 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
372 /* ASSERT((Size & (PAGE_SIZE-1)) == 0); */
374 pagecount = size >> PAGE_SHIFT;
375 pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
377 /* do we need a gpadl body msg */
378 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
379 sizeof(struct vmbus_channel_gpadl_header) -
380 sizeof(struct gpa_range);
381 pfncount = pfnsize / sizeof(u64);
383 if (pagecount > pfncount) {
384 /* we need a gpadl body */
385 /* fill in the header */
386 msgsize = sizeof(struct vmbus_channel_msginfo) +
387 sizeof(struct vmbus_channel_gpadl_header) +
388 sizeof(struct gpa_range) + pfncount * sizeof(u64);
389 msgheader = kzalloc(msgsize, GFP_KERNEL);
393 INIT_LIST_HEAD(&msgheader->SubMsgList);
394 msgheader->MessageSize = msgsize;
396 gpadl_header = (struct vmbus_channel_gpadl_header *)
398 gpadl_header->RangeCount = 1;
399 gpadl_header->RangeBufLen = sizeof(struct gpa_range) +
400 pagecount * sizeof(u64);
401 gpadl_header->Range[0].ByteOffset = 0;
402 gpadl_header->Range[0].ByteCount = size;
403 for (i = 0; i < pfncount; i++)
404 gpadl_header->Range[0].PfnArray[i] = pfn+i;
405 *msginfo = msgheader;
409 pfnleft = pagecount - pfncount;
411 /* how many pfns can we fit */
412 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
413 sizeof(struct vmbus_channel_gpadl_body);
414 pfncount = pfnsize / sizeof(u64);
416 /* fill in the body */
418 if (pfnleft > pfncount)
423 msgsize = sizeof(struct vmbus_channel_msginfo) +
424 sizeof(struct vmbus_channel_gpadl_body) +
425 pfncurr * sizeof(u64);
426 msgbody = kzalloc(msgsize, GFP_KERNEL);
427 /* FIXME: we probably need to more if this fails */
430 msgbody->MessageSize = msgsize;
433 (struct vmbus_channel_gpadl_body *)msgbody->Msg;
437 * Gpadl is u32 and we are using a pointer which could
440 /* gpadl_body->Gpadl = kbuffer; */
441 for (i = 0; i < pfncurr; i++)
442 gpadl_body->Pfn[i] = pfn + pfnsum + i;
444 /* add to msg header */
445 list_add_tail(&msgbody->MsgListEntry,
446 &msgheader->SubMsgList);
451 /* everything fits in a header */
452 msgsize = sizeof(struct vmbus_channel_msginfo) +
453 sizeof(struct vmbus_channel_gpadl_header) +
454 sizeof(struct gpa_range) + pagecount * sizeof(u64);
455 msgheader = kzalloc(msgsize, GFP_KERNEL);
456 if (msgheader == NULL)
458 msgheader->MessageSize = msgsize;
460 gpadl_header = (struct vmbus_channel_gpadl_header *)
462 gpadl_header->RangeCount = 1;
463 gpadl_header->RangeBufLen = sizeof(struct gpa_range) +
464 pagecount * sizeof(u64);
465 gpadl_header->Range[0].ByteOffset = 0;
466 gpadl_header->Range[0].ByteCount = size;
467 for (i = 0; i < pagecount; i++)
468 gpadl_header->Range[0].PfnArray[i] = pfn+i;
470 *msginfo = msgheader;
482 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
484 * @channel: a channel
485 * @kbuffer: from kmalloc
486 * @size: page-size multiple
487 * @gpadl_handle: some funky thing
489 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
490 u32 size, u32 *gpadl_handle)
492 struct vmbus_channel_gpadl_header *gpadlmsg;
493 struct vmbus_channel_gpadl_body *gpadl_body;
494 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
495 struct vmbus_channel_msginfo *msginfo = NULL;
496 struct vmbus_channel_msginfo *submsginfo;
498 struct list_head *curr;
499 u32 next_gpadl_handle;
503 next_gpadl_handle = atomic_read(&gVmbusConnection.NextGpadlHandle);
504 atomic_inc(&gVmbusConnection.NextGpadlHandle);
506 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
510 msginfo->WaitEvent = osd_WaitEventCreate();
511 if (!msginfo->WaitEvent) {
516 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->Msg;
517 gpadlmsg->Header.MessageType = ChannelMessageGpadlHeader;
518 gpadlmsg->ChildRelId = channel->OfferMsg.ChildRelId;
519 gpadlmsg->Gpadl = next_gpadl_handle;
521 dump_gpadl_header(gpadlmsg);
523 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
524 list_add_tail(&msginfo->MsgListEntry,
525 &gVmbusConnection.ChannelMsgList);
527 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
528 DPRINT_DBG(VMBUS, "buffer %p, size %d msg cnt %d",
529 kbuffer, size, msgcount);
531 DPRINT_DBG(VMBUS, "Sending GPADL Header - len %zd",
532 msginfo->MessageSize - sizeof(*msginfo));
534 ret = VmbusPostMessage(gpadlmsg, msginfo->MessageSize -
537 DPRINT_ERR(VMBUS, "Unable to open channel - %d", ret);
542 list_for_each(curr, &msginfo->SubMsgList) {
544 /* FIXME: should this use list_entry() instead ? */
545 submsginfo = (struct vmbus_channel_msginfo *)curr;
547 (struct vmbus_channel_gpadl_body *)submsginfo->Msg;
549 gpadl_body->Header.MessageType =
550 ChannelMessageGpadlBody;
551 gpadl_body->Gpadl = next_gpadl_handle;
553 DPRINT_DBG(VMBUS, "Sending GPADL Body - len %zd",
554 submsginfo->MessageSize -
555 sizeof(*submsginfo));
557 dump_gpadl_body(gpadl_body, submsginfo->MessageSize -
558 sizeof(*submsginfo));
559 ret = VmbusPostMessage(gpadl_body,
560 submsginfo->MessageSize -
561 sizeof(*submsginfo));
567 osd_WaitEventWait(msginfo->WaitEvent);
569 /* At this point, we received the gpadl created msg */
570 DPRINT_DBG(VMBUS, "Received GPADL created "
571 "(relid %d, status %d handle %x)",
572 channel->OfferMsg.ChildRelId,
573 msginfo->Response.GpadlCreated.CreationStatus,
576 *gpadl_handle = gpadlmsg->Gpadl;
579 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
580 list_del(&msginfo->MsgListEntry);
581 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
583 kfree(msginfo->WaitEvent);
589 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
591 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
593 struct vmbus_channel_gpadl_teardown *msg;
594 struct vmbus_channel_msginfo *info;
598 /* ASSERT(gpadl_handle != 0); */
600 info = kmalloc(sizeof(*info) +
601 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
605 info->WaitEvent = osd_WaitEventCreate();
606 if (!info->WaitEvent) {
611 msg = (struct vmbus_channel_gpadl_teardown *)info->Msg;
613 msg->Header.MessageType = ChannelMessageGpadlTeardown;
614 msg->ChildRelId = channel->OfferMsg.ChildRelId;
615 msg->Gpadl = gpadl_handle;
617 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
618 list_add_tail(&info->MsgListEntry,
619 &gVmbusConnection.ChannelMsgList);
620 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
622 ret = VmbusPostMessage(msg,
623 sizeof(struct vmbus_channel_gpadl_teardown));
629 osd_WaitEventWait(info->WaitEvent);
631 /* Received a torndown response */
632 spin_lock_irqsave(&gVmbusConnection.channelmsg_lock, flags);
633 list_del(&info->MsgListEntry);
634 spin_unlock_irqrestore(&gVmbusConnection.channelmsg_lock, flags);
636 kfree(info->WaitEvent);
642 * vmbus_close - Close the specified channel
644 void vmbus_close(struct vmbus_channel *channel)
646 struct vmbus_channel_close_channel *msg;
647 struct vmbus_channel_msginfo *info;
651 /* Stop callback and cancel the timer asap */
652 channel->OnChannelCallback = NULL;
653 del_timer_sync(&channel->poll_timer);
655 /* Send a closing message */
656 info = kmalloc(sizeof(*info) +
657 sizeof(struct vmbus_channel_close_channel), GFP_KERNEL);
658 /* FIXME: can't do anything other than return here because the
659 * function is void */
663 /* info->waitEvent = osd_WaitEventCreate(); */
665 msg = (struct vmbus_channel_close_channel *)info->Msg;
666 msg->Header.MessageType = ChannelMessageCloseChannel;
667 msg->ChildRelId = channel->OfferMsg.ChildRelId;
669 ret = VmbusPostMessage(msg, sizeof(struct vmbus_channel_close_channel));
675 /* Tear down the gpadl for the channel's ring buffer */
676 if (channel->RingBufferGpadlHandle)
677 vmbus_teardown_gpadl(channel,
678 channel->RingBufferGpadlHandle);
680 /* TODO: Send a msg to release the childRelId */
682 /* Cleanup the ring buffers for this channel */
683 RingBufferCleanup(&channel->Outbound);
684 RingBufferCleanup(&channel->Inbound);
686 osd_PageFree(channel->RingBufferPages, channel->RingBufferPageCount);
691 * If we are closing the channel during an error path in
692 * opening the channel, don't free the channel since the
693 * caller will free the channel
696 if (channel->State == CHANNEL_OPEN_STATE) {
697 spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
698 list_del(&channel->ListEntry);
699 spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
701 free_channel(channel);
706 * vmbus_sendpacket() - Send the specified buffer on the given channel
707 * @channel: Pointer to vmbus_channel structure.
708 * @buffer: Pointer to the buffer you want to receive the data into.
709 * @bufferlen: Maximum size of what the the buffer will hold
710 * @requestid: Identifier of the request
711 * @type: Type of packet that is being send e.g. negotiate, time
714 * Sends data in @buffer directly to hyper-v via the vmbus
715 * This will send the data unparsed to hyper-v.
717 * Mainly used by Hyper-V drivers.
719 int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
720 u32 bufferlen, u64 requestid,
721 enum vmbus_packet_type type, u32 flags)
723 struct vmpacket_descriptor desc;
724 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
725 u32 packetlen_aligned = ALIGN_UP(packetlen, sizeof(u64));
726 struct scatterlist bufferlist[3];
727 u64 aligned_data = 0;
730 DPRINT_DBG(VMBUS, "channel %p buffer %p len %d",
731 channel, buffer, bufferlen);
733 dump_vmbus_channel(channel);
735 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
737 /* Setup the descriptor */
738 desc.Type = type; /* VmbusPacketTypeDataInBand; */
739 desc.Flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
740 /* in 8-bytes granularity */
741 desc.DataOffset8 = sizeof(struct vmpacket_descriptor) >> 3;
742 desc.Length8 = (u16)(packetlen_aligned >> 3);
743 desc.TransactionId = requestid;
745 sg_init_table(bufferlist, 3);
746 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
747 sg_set_buf(&bufferlist[1], buffer, bufferlen);
748 sg_set_buf(&bufferlist[2], &aligned_data,
749 packetlen_aligned - packetlen);
751 ret = RingBufferWrite(&channel->Outbound, bufferlist, 3);
753 /* TODO: We should determine if this is optional */
754 if (ret == 0 && !GetRingBufferInterruptMask(&channel->Outbound))
755 vmbus_setevent(channel);
759 EXPORT_SYMBOL(vmbus_sendpacket);
762 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
763 * packets using a GPADL Direct packet type.
765 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
766 struct hv_page_buffer pagebuffers[],
767 u32 pagecount, void *buffer, u32 bufferlen,
772 struct vmbus_channel_packet_page_buffer desc;
775 u32 packetlen_aligned;
776 struct scatterlist bufferlist[3];
777 u64 aligned_data = 0;
779 if (pagecount > MAX_PAGE_BUFFER_COUNT)
782 dump_vmbus_channel(channel);
785 * Adjust the size down since vmbus_channel_packet_page_buffer is the
786 * largest size we support
788 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
789 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
790 sizeof(struct hv_page_buffer));
791 packetlen = descsize + bufferlen;
792 packetlen_aligned = ALIGN_UP(packetlen, sizeof(u64));
794 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
796 /* Setup the descriptor */
797 desc.type = VmbusPacketTypeDataUsingGpaDirect;
798 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
799 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
800 desc.length8 = (u16)(packetlen_aligned >> 3);
801 desc.transactionid = requestid;
802 desc.rangecount = pagecount;
804 for (i = 0; i < pagecount; i++) {
805 desc.range[i].Length = pagebuffers[i].Length;
806 desc.range[i].Offset = pagebuffers[i].Offset;
807 desc.range[i].Pfn = pagebuffers[i].Pfn;
810 sg_init_table(bufferlist, 3);
811 sg_set_buf(&bufferlist[0], &desc, descsize);
812 sg_set_buf(&bufferlist[1], buffer, bufferlen);
813 sg_set_buf(&bufferlist[2], &aligned_data,
814 packetlen_aligned - packetlen);
816 ret = RingBufferWrite(&channel->Outbound, bufferlist, 3);
818 /* TODO: We should determine if this is optional */
819 if (ret == 0 && !GetRingBufferInterruptMask(&channel->Outbound))
820 vmbus_setevent(channel);
826 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
827 * using a GPADL Direct packet type.
829 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
830 struct hv_multipage_buffer *multi_pagebuffer,
831 void *buffer, u32 bufferlen, u64 requestid)
834 struct vmbus_channel_packet_multipage_buffer desc;
837 u32 packetlen_aligned;
838 struct scatterlist bufferlist[3];
839 u64 aligned_data = 0;
840 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->Offset,
841 multi_pagebuffer->Length);
843 dump_vmbus_channel(channel);
845 DPRINT_DBG(VMBUS, "data buffer - offset %u len %u pfn count %u",
846 multi_pagebuffer->Offset,
847 multi_pagebuffer->Length, pfncount);
849 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
853 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
854 * the largest size we support
856 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
857 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
859 packetlen = descsize + bufferlen;
860 packetlen_aligned = ALIGN_UP(packetlen, sizeof(u64));
862 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
864 /* Setup the descriptor */
865 desc.type = VmbusPacketTypeDataUsingGpaDirect;
866 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
867 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
868 desc.length8 = (u16)(packetlen_aligned >> 3);
869 desc.transactionid = requestid;
872 desc.range.Length = multi_pagebuffer->Length;
873 desc.range.Offset = multi_pagebuffer->Offset;
875 memcpy(desc.range.PfnArray, multi_pagebuffer->PfnArray,
876 pfncount * sizeof(u64));
878 sg_init_table(bufferlist, 3);
879 sg_set_buf(&bufferlist[0], &desc, descsize);
880 sg_set_buf(&bufferlist[1], buffer, bufferlen);
881 sg_set_buf(&bufferlist[2], &aligned_data,
882 packetlen_aligned - packetlen);
884 ret = RingBufferWrite(&channel->Outbound, bufferlist, 3);
886 /* TODO: We should determine if this is optional */
887 if (ret == 0 && !GetRingBufferInterruptMask(&channel->Outbound))
888 vmbus_setevent(channel);
895 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
896 * @channel: Pointer to vmbus_channel structure.
897 * @buffer: Pointer to the buffer you want to receive the data into.
898 * @bufferlen: Maximum size of what the the buffer will hold
899 * @buffer_actual_len: The actual size of the data after it was received
900 * @requestid: Identifier of the request
902 * Receives directly from the hyper-v vmbus and puts the data it received
903 * into Buffer. This will receive the data unparsed from hyper-v.
905 * Mainly used by Hyper-V drivers.
907 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
908 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
910 struct vmpacket_descriptor desc;
916 *buffer_actual_len = 0;
919 spin_lock_irqsave(&channel->inbound_lock, flags);
921 ret = RingBufferPeek(&channel->Inbound, &desc,
922 sizeof(struct vmpacket_descriptor));
924 spin_unlock_irqrestore(&channel->inbound_lock, flags);
926 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
930 /* VmbusChannelClearEvent(Channel); */
932 packetlen = desc.Length8 << 3;
933 userlen = packetlen - (desc.DataOffset8 << 3);
934 /* ASSERT(userLen > 0); */
936 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d "
937 "flag %d tid %llx pktlen %d datalen %d> ",
938 channel, channel->OfferMsg.ChildRelId, desc.Type,
939 desc.Flags, desc.TransactionId, packetlen, userlen);
941 *buffer_actual_len = userlen;
943 if (userlen > bufferlen) {
944 spin_unlock_irqrestore(&channel->inbound_lock, flags);
946 DPRINT_ERR(VMBUS, "buffer too small - got %d needs %d",
951 *requestid = desc.TransactionId;
953 /* Copy over the packet to the user buffer */
954 ret = RingBufferRead(&channel->Inbound, buffer, userlen,
955 (desc.DataOffset8 << 3));
957 spin_unlock_irqrestore(&channel->inbound_lock, flags);
961 EXPORT_SYMBOL(vmbus_recvpacket);
964 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
966 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
967 u32 bufferlen, u32 *buffer_actual_len,
970 struct vmpacket_descriptor desc;
976 *buffer_actual_len = 0;
979 spin_lock_irqsave(&channel->inbound_lock, flags);
981 ret = RingBufferPeek(&channel->Inbound, &desc,
982 sizeof(struct vmpacket_descriptor));
984 spin_unlock_irqrestore(&channel->inbound_lock, flags);
986 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
990 /* VmbusChannelClearEvent(Channel); */
992 packetlen = desc.Length8 << 3;
993 userlen = packetlen - (desc.DataOffset8 << 3);
995 DPRINT_DBG(VMBUS, "packet received on channel %p relid %d <type %d "
996 "flag %d tid %llx pktlen %d datalen %d> ",
997 channel, channel->OfferMsg.ChildRelId, desc.Type,
998 desc.Flags, desc.TransactionId, packetlen, userlen);
1000 *buffer_actual_len = packetlen;
1002 if (packetlen > bufferlen) {
1003 spin_unlock_irqrestore(&channel->inbound_lock, flags);
1005 DPRINT_ERR(VMBUS, "buffer too small - needed %d bytes but "
1006 "got space for only %d bytes", packetlen, bufferlen);
1010 *requestid = desc.TransactionId;
1012 /* Copy over the entire packet to the user buffer */
1013 ret = RingBufferRead(&channel->Inbound, buffer, packetlen, 0);
1015 spin_unlock_irqrestore(&channel->inbound_lock, flags);
1020 * vmbus_onchannel_event - Channel event callback
1022 void vmbus_onchannel_event(struct vmbus_channel *channel)
1024 dump_vmbus_channel(channel);
1025 /* ASSERT(Channel->OnChannelCallback); */
1027 channel->OnChannelCallback(channel->ChannelCallbackContext);
1029 mod_timer(&channel->poll_timer, jiffies + usecs_to_jiffies(100));
1033 * vmbus_ontimer - Timer event callback
1035 void vmbus_ontimer(unsigned long data)
1037 struct vmbus_channel *channel = (struct vmbus_channel *)data;
1039 if (channel->OnChannelCallback)
1040 channel->OnChannelCallback(channel->ChannelCallbackContext);
1044 * dump_vmbus_channel- Dump vmbus channel info to the console
1046 static void dump_vmbus_channel(struct vmbus_channel *channel)
1048 DPRINT_DBG(VMBUS, "Channel (%d)", channel->OfferMsg.ChildRelId);
1049 DumpRingInfo(&channel->Outbound, "Outbound ");
1050 DumpRingInfo(&channel->Inbound, "Inbound ");