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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/wait.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/hyperv.h>
31 #include "hyperv_vmbus.h"
33 #define NUM_PAGES_SPANNED(addr, len) \
34 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
37 * vmbus_setevent- Trigger an event notification on the specified
40 static void vmbus_setevent(struct vmbus_channel *channel)
42 struct hv_monitor_page *monitorpage;
44 if (channel->offermsg.monitor_allocated) {
45 /* Each u32 represents 32 channels */
46 sync_set_bit(channel->offermsg.child_relid & 31,
47 (unsigned long *) vmbus_connection.send_int_page +
48 (channel->offermsg.child_relid >> 5));
50 monitorpage = vmbus_connection.monitor_pages;
51 monitorpage++; /* Get the child to parent monitor page */
53 sync_set_bit(channel->monitor_bit,
54 (unsigned long *)&monitorpage->trigger_group
55 [channel->monitor_grp].pending);
58 vmbus_set_event(channel);
63 * vmbus_get_debug_info -Retrieve various channel debug info
65 void vmbus_get_debug_info(struct vmbus_channel *channel,
66 struct vmbus_channel_debug_info *debuginfo)
68 struct hv_monitor_page *monitorpage;
69 u8 monitor_group = (u8)channel->offermsg.monitorid / 32;
70 u8 monitor_offset = (u8)channel->offermsg.monitorid % 32;
72 monitorpage = (struct hv_monitor_page *)vmbus_connection.monitor_pages;
74 debuginfo->servermonitor_pending =
75 monitorpage->trigger_group[monitor_group].pending;
76 debuginfo->servermonitor_latency =
77 monitorpage->latency[monitor_group][monitor_offset];
78 debuginfo->servermonitor_connectionid =
79 monitorpage->parameter[monitor_group]
80 [monitor_offset].connectionid.u.id;
84 debuginfo->clientmonitor_pending =
85 monitorpage->trigger_group[monitor_group].pending;
86 debuginfo->clientmonitor_latency =
87 monitorpage->latency[monitor_group][monitor_offset];
88 debuginfo->clientmonitor_connectionid =
89 monitorpage->parameter[monitor_group]
90 [monitor_offset].connectionid.u.id;
92 hv_ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound);
93 hv_ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound);
97 * vmbus_open - Open the specified channel.
99 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
100 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
101 void (*onchannelcallback)(void *context), void *context)
103 struct vmbus_channel_open_channel *open_msg;
104 struct vmbus_channel_msginfo *open_info = NULL;
109 spin_lock_irqsave(&newchannel->sc_lock, flags);
110 if (newchannel->state == CHANNEL_OPEN_STATE) {
111 newchannel->state = CHANNEL_OPENING_STATE;
113 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
116 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
118 newchannel->onchannel_callback = onchannelcallback;
119 newchannel->channel_callback_context = context;
121 /* Allocate the ring buffer */
122 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
123 get_order(send_ringbuffer_size + recv_ringbuffer_size));
129 in = (void *)((unsigned long)out + send_ringbuffer_size);
131 newchannel->ringbuffer_pages = out;
132 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
133 recv_ringbuffer_size) >> PAGE_SHIFT;
135 ret = hv_ringbuffer_init(
136 &newchannel->outbound, out, send_ringbuffer_size);
143 ret = hv_ringbuffer_init(
144 &newchannel->inbound, in, recv_ringbuffer_size);
151 /* Establish the gpadl for the ring buffer */
152 newchannel->ringbuffer_gpadlhandle = 0;
154 ret = vmbus_establish_gpadl(newchannel,
155 newchannel->outbound.ring_buffer,
156 send_ringbuffer_size +
157 recv_ringbuffer_size,
158 &newchannel->ringbuffer_gpadlhandle);
165 /* Create and init the channel open message */
166 open_info = kmalloc(sizeof(*open_info) +
167 sizeof(struct vmbus_channel_open_channel),
174 init_completion(&open_info->waitevent);
176 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
177 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
178 open_msg->openid = newchannel->offermsg.child_relid;
179 open_msg->child_relid = newchannel->offermsg.child_relid;
180 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
181 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
183 open_msg->target_vp = newchannel->target_vp;
185 if (userdatalen > MAX_USER_DEFINED_BYTES) {
191 memcpy(open_msg->userdata, userdata, userdatalen);
193 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
194 list_add_tail(&open_info->msglistentry,
195 &vmbus_connection.chn_msg_list);
196 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
198 ret = vmbus_post_msg(open_msg,
199 sizeof(struct vmbus_channel_open_channel));
204 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
211 if (open_info->response.open_result.status)
212 err = open_info->response.open_result.status;
214 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
215 list_del(&open_info->msglistentry);
216 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
219 newchannel->state = CHANNEL_OPENED_STATE;
225 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
226 list_del(&open_info->msglistentry);
227 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
230 free_pages((unsigned long)out,
231 get_order(send_ringbuffer_size + recv_ringbuffer_size));
235 EXPORT_SYMBOL_GPL(vmbus_open);
238 * create_gpadl_header - Creates a gpadl for the specified buffer
240 static int create_gpadl_header(void *kbuffer, u32 size,
241 struct vmbus_channel_msginfo **msginfo,
246 unsigned long long pfn;
247 struct vmbus_channel_gpadl_header *gpadl_header;
248 struct vmbus_channel_gpadl_body *gpadl_body;
249 struct vmbus_channel_msginfo *msgheader;
250 struct vmbus_channel_msginfo *msgbody = NULL;
253 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
255 pagecount = size >> PAGE_SHIFT;
256 pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
258 /* do we need a gpadl body msg */
259 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
260 sizeof(struct vmbus_channel_gpadl_header) -
261 sizeof(struct gpa_range);
262 pfncount = pfnsize / sizeof(u64);
264 if (pagecount > pfncount) {
265 /* we need a gpadl body */
266 /* fill in the header */
267 msgsize = sizeof(struct vmbus_channel_msginfo) +
268 sizeof(struct vmbus_channel_gpadl_header) +
269 sizeof(struct gpa_range) + pfncount * sizeof(u64);
270 msgheader = kzalloc(msgsize, GFP_KERNEL);
274 INIT_LIST_HEAD(&msgheader->submsglist);
275 msgheader->msgsize = msgsize;
277 gpadl_header = (struct vmbus_channel_gpadl_header *)
279 gpadl_header->rangecount = 1;
280 gpadl_header->range_buflen = sizeof(struct gpa_range) +
281 pagecount * sizeof(u64);
282 gpadl_header->range[0].byte_offset = 0;
283 gpadl_header->range[0].byte_count = size;
284 for (i = 0; i < pfncount; i++)
285 gpadl_header->range[0].pfn_array[i] = pfn+i;
286 *msginfo = msgheader;
290 pfnleft = pagecount - pfncount;
292 /* how many pfns can we fit */
293 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
294 sizeof(struct vmbus_channel_gpadl_body);
295 pfncount = pfnsize / sizeof(u64);
297 /* fill in the body */
299 if (pfnleft > pfncount)
304 msgsize = sizeof(struct vmbus_channel_msginfo) +
305 sizeof(struct vmbus_channel_gpadl_body) +
306 pfncurr * sizeof(u64);
307 msgbody = kzalloc(msgsize, GFP_KERNEL);
310 struct vmbus_channel_msginfo *pos = NULL;
311 struct vmbus_channel_msginfo *tmp = NULL;
313 * Free up all the allocated messages.
315 list_for_each_entry_safe(pos, tmp,
316 &msgheader->submsglist,
319 list_del(&pos->msglistentry);
326 msgbody->msgsize = msgsize;
329 (struct vmbus_channel_gpadl_body *)msgbody->msg;
332 * Gpadl is u32 and we are using a pointer which could
334 * This is governed by the guest/host protocol and
335 * so the hypervisor gurantees that this is ok.
337 for (i = 0; i < pfncurr; i++)
338 gpadl_body->pfn[i] = pfn + pfnsum + i;
340 /* add to msg header */
341 list_add_tail(&msgbody->msglistentry,
342 &msgheader->submsglist);
347 /* everything fits in a header */
348 msgsize = sizeof(struct vmbus_channel_msginfo) +
349 sizeof(struct vmbus_channel_gpadl_header) +
350 sizeof(struct gpa_range) + pagecount * sizeof(u64);
351 msgheader = kzalloc(msgsize, GFP_KERNEL);
352 if (msgheader == NULL)
354 msgheader->msgsize = msgsize;
356 gpadl_header = (struct vmbus_channel_gpadl_header *)
358 gpadl_header->rangecount = 1;
359 gpadl_header->range_buflen = sizeof(struct gpa_range) +
360 pagecount * sizeof(u64);
361 gpadl_header->range[0].byte_offset = 0;
362 gpadl_header->range[0].byte_count = size;
363 for (i = 0; i < pagecount; i++)
364 gpadl_header->range[0].pfn_array[i] = pfn+i;
366 *msginfo = msgheader;
378 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
380 * @channel: a channel
381 * @kbuffer: from kmalloc
382 * @size: page-size multiple
383 * @gpadl_handle: some funky thing
385 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
386 u32 size, u32 *gpadl_handle)
388 struct vmbus_channel_gpadl_header *gpadlmsg;
389 struct vmbus_channel_gpadl_body *gpadl_body;
390 struct vmbus_channel_msginfo *msginfo = NULL;
391 struct vmbus_channel_msginfo *submsginfo;
393 struct list_head *curr;
394 u32 next_gpadl_handle;
399 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
400 atomic_inc(&vmbus_connection.next_gpadl_handle);
402 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
406 init_completion(&msginfo->waitevent);
408 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
409 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
410 gpadlmsg->child_relid = channel->offermsg.child_relid;
411 gpadlmsg->gpadl = next_gpadl_handle;
414 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
415 list_add_tail(&msginfo->msglistentry,
416 &vmbus_connection.chn_msg_list);
418 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
420 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
426 list_for_each(curr, &msginfo->submsglist) {
428 submsginfo = (struct vmbus_channel_msginfo *)curr;
430 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
432 gpadl_body->header.msgtype =
433 CHANNELMSG_GPADL_BODY;
434 gpadl_body->gpadl = next_gpadl_handle;
436 ret = vmbus_post_msg(gpadl_body,
437 submsginfo->msgsize -
438 sizeof(*submsginfo));
444 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
448 /* At this point, we received the gpadl created msg */
449 *gpadl_handle = gpadlmsg->gpadl;
452 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
453 list_del(&msginfo->msglistentry);
454 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
459 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
462 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
464 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
466 struct vmbus_channel_gpadl_teardown *msg;
467 struct vmbus_channel_msginfo *info;
471 info = kmalloc(sizeof(*info) +
472 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
476 init_completion(&info->waitevent);
478 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
480 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
481 msg->child_relid = channel->offermsg.child_relid;
482 msg->gpadl = gpadl_handle;
484 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
485 list_add_tail(&info->msglistentry,
486 &vmbus_connection.chn_msg_list);
487 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
488 ret = vmbus_post_msg(msg,
489 sizeof(struct vmbus_channel_gpadl_teardown));
492 t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
495 /* Received a torndown response */
496 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
497 list_del(&info->msglistentry);
498 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
503 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
505 static void vmbus_close_internal(struct vmbus_channel *channel)
507 struct vmbus_channel_close_channel *msg;
511 channel->state = CHANNEL_OPEN_STATE;
512 channel->sc_creation_callback = NULL;
513 /* Stop callback and cancel the timer asap */
514 spin_lock_irqsave(&channel->inbound_lock, flags);
515 channel->onchannel_callback = NULL;
516 spin_unlock_irqrestore(&channel->inbound_lock, flags);
518 /* Send a closing message */
520 msg = &channel->close_msg.msg;
522 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
523 msg->child_relid = channel->offermsg.child_relid;
525 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
528 /* Tear down the gpadl for the channel's ring buffer */
529 if (channel->ringbuffer_gpadlhandle)
530 vmbus_teardown_gpadl(channel,
531 channel->ringbuffer_gpadlhandle);
533 /* Cleanup the ring buffers for this channel */
534 hv_ringbuffer_cleanup(&channel->outbound);
535 hv_ringbuffer_cleanup(&channel->inbound);
537 free_pages((unsigned long)channel->ringbuffer_pages,
538 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
544 * vmbus_close - Close the specified channel
546 void vmbus_close(struct vmbus_channel *channel)
548 struct list_head *cur, *tmp;
549 struct vmbus_channel *cur_channel;
551 if (channel->primary_channel != NULL) {
553 * We will only close sub-channels when
554 * the primary is closed.
559 * Close all the sub-channels first and then close the
562 list_for_each_safe(cur, tmp, &channel->sc_list) {
563 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
564 if (cur_channel->state != CHANNEL_OPENED_STATE)
566 vmbus_close_internal(cur_channel);
569 * Now close the primary.
571 vmbus_close_internal(channel);
573 EXPORT_SYMBOL_GPL(vmbus_close);
576 * vmbus_sendpacket() - Send the specified buffer on the given channel
577 * @channel: Pointer to vmbus_channel structure.
578 * @buffer: Pointer to the buffer you want to receive the data into.
579 * @bufferlen: Maximum size of what the the buffer will hold
580 * @requestid: Identifier of the request
581 * @type: Type of packet that is being send e.g. negotiate, time
584 * Sends data in @buffer directly to hyper-v via the vmbus
585 * This will send the data unparsed to hyper-v.
587 * Mainly used by Hyper-V drivers.
589 int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
590 u32 bufferlen, u64 requestid,
591 enum vmbus_packet_type type, u32 flags)
593 struct vmpacket_descriptor desc;
594 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
595 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
596 struct scatterlist bufferlist[3];
597 u64 aligned_data = 0;
602 /* Setup the descriptor */
603 desc.type = type; /* VmbusPacketTypeDataInBand; */
604 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
605 /* in 8-bytes granularity */
606 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
607 desc.len8 = (u16)(packetlen_aligned >> 3);
608 desc.trans_id = requestid;
610 sg_init_table(bufferlist, 3);
611 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
612 sg_set_buf(&bufferlist[1], buffer, bufferlen);
613 sg_set_buf(&bufferlist[2], &aligned_data,
614 packetlen_aligned - packetlen);
616 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
618 if (ret == 0 && signal)
619 vmbus_setevent(channel);
623 EXPORT_SYMBOL(vmbus_sendpacket);
626 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
627 * packets using a GPADL Direct packet type.
629 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
630 struct hv_page_buffer pagebuffers[],
631 u32 pagecount, void *buffer, u32 bufferlen,
636 struct vmbus_channel_packet_page_buffer desc;
639 u32 packetlen_aligned;
640 struct scatterlist bufferlist[3];
641 u64 aligned_data = 0;
644 if (pagecount > MAX_PAGE_BUFFER_COUNT)
649 * Adjust the size down since vmbus_channel_packet_page_buffer is the
650 * largest size we support
652 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
653 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
654 sizeof(struct hv_page_buffer));
655 packetlen = descsize + bufferlen;
656 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
658 /* Setup the descriptor */
659 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
660 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
661 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
662 desc.length8 = (u16)(packetlen_aligned >> 3);
663 desc.transactionid = requestid;
664 desc.rangecount = pagecount;
666 for (i = 0; i < pagecount; i++) {
667 desc.range[i].len = pagebuffers[i].len;
668 desc.range[i].offset = pagebuffers[i].offset;
669 desc.range[i].pfn = pagebuffers[i].pfn;
672 sg_init_table(bufferlist, 3);
673 sg_set_buf(&bufferlist[0], &desc, descsize);
674 sg_set_buf(&bufferlist[1], buffer, bufferlen);
675 sg_set_buf(&bufferlist[2], &aligned_data,
676 packetlen_aligned - packetlen);
678 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
680 if (ret == 0 && signal)
681 vmbus_setevent(channel);
685 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
688 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
689 * using a GPADL Direct packet type.
691 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
692 struct hv_multipage_buffer *multi_pagebuffer,
693 void *buffer, u32 bufferlen, u64 requestid)
696 struct vmbus_channel_packet_multipage_buffer desc;
699 u32 packetlen_aligned;
700 struct scatterlist bufferlist[3];
701 u64 aligned_data = 0;
703 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
704 multi_pagebuffer->len);
707 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
711 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
712 * the largest size we support
714 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
715 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
717 packetlen = descsize + bufferlen;
718 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
721 /* Setup the descriptor */
722 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
723 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
724 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
725 desc.length8 = (u16)(packetlen_aligned >> 3);
726 desc.transactionid = requestid;
729 desc.range.len = multi_pagebuffer->len;
730 desc.range.offset = multi_pagebuffer->offset;
732 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
733 pfncount * sizeof(u64));
735 sg_init_table(bufferlist, 3);
736 sg_set_buf(&bufferlist[0], &desc, descsize);
737 sg_set_buf(&bufferlist[1], buffer, bufferlen);
738 sg_set_buf(&bufferlist[2], &aligned_data,
739 packetlen_aligned - packetlen);
741 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
743 if (ret == 0 && signal)
744 vmbus_setevent(channel);
748 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
751 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
752 * @channel: Pointer to vmbus_channel structure.
753 * @buffer: Pointer to the buffer you want to receive the data into.
754 * @bufferlen: Maximum size of what the the buffer will hold
755 * @buffer_actual_len: The actual size of the data after it was received
756 * @requestid: Identifier of the request
758 * Receives directly from the hyper-v vmbus and puts the data it received
759 * into Buffer. This will receive the data unparsed from hyper-v.
761 * Mainly used by Hyper-V drivers.
763 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
764 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
766 struct vmpacket_descriptor desc;
772 *buffer_actual_len = 0;
776 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
777 sizeof(struct vmpacket_descriptor));
781 packetlen = desc.len8 << 3;
782 userlen = packetlen - (desc.offset8 << 3);
784 *buffer_actual_len = userlen;
786 if (userlen > bufferlen) {
788 pr_err("Buffer too small - got %d needs %d\n",
793 *requestid = desc.trans_id;
795 /* Copy over the packet to the user buffer */
796 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
797 (desc.offset8 << 3), &signal);
800 vmbus_setevent(channel);
804 EXPORT_SYMBOL(vmbus_recvpacket);
807 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
809 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
810 u32 bufferlen, u32 *buffer_actual_len,
813 struct vmpacket_descriptor desc;
818 *buffer_actual_len = 0;
822 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
823 sizeof(struct vmpacket_descriptor));
828 packetlen = desc.len8 << 3;
830 *buffer_actual_len = packetlen;
832 if (packetlen > bufferlen) {
833 pr_err("Buffer too small - needed %d bytes but "
834 "got space for only %d bytes\n",
835 packetlen, bufferlen);
839 *requestid = desc.trans_id;
841 /* Copy over the entire packet to the user buffer */
842 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
846 vmbus_setevent(channel);
850 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);