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 /* Get the child to parent monitor page */
51 monitorpage = vmbus_connection.monitor_pages[1];
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 = vmbus_connection.monitor_pages[0];
73 debuginfo->servermonitor_latency =
74 monitorpage->latency[monitor_group][monitor_offset];
75 debuginfo->servermonitor_connectionid =
76 monitorpage->parameter[monitor_group]
77 [monitor_offset].connectionid.u.id;
79 monitorpage = vmbus_connection.monitor_pages[1];
80 debuginfo->clientmonitor_latency =
81 monitorpage->latency[monitor_group][monitor_offset];
82 debuginfo->clientmonitor_connectionid =
83 monitorpage->parameter[monitor_group]
84 [monitor_offset].connectionid.u.id;
86 hv_ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound);
87 hv_ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound);
91 * vmbus_open - Open the specified channel.
93 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
94 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
95 void (*onchannelcallback)(void *context), void *context)
97 struct vmbus_channel_open_channel *open_msg;
98 struct vmbus_channel_msginfo *open_info = NULL;
103 spin_lock_irqsave(&newchannel->sc_lock, flags);
104 if (newchannel->state == CHANNEL_OPEN_STATE) {
105 newchannel->state = CHANNEL_OPENING_STATE;
107 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
110 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
112 newchannel->onchannel_callback = onchannelcallback;
113 newchannel->channel_callback_context = context;
115 /* Allocate the ring buffer */
116 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
117 get_order(send_ringbuffer_size + recv_ringbuffer_size));
123 in = (void *)((unsigned long)out + send_ringbuffer_size);
125 newchannel->ringbuffer_pages = out;
126 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
127 recv_ringbuffer_size) >> PAGE_SHIFT;
129 ret = hv_ringbuffer_init(
130 &newchannel->outbound, out, send_ringbuffer_size);
137 ret = hv_ringbuffer_init(
138 &newchannel->inbound, in, recv_ringbuffer_size);
145 /* Establish the gpadl for the ring buffer */
146 newchannel->ringbuffer_gpadlhandle = 0;
148 ret = vmbus_establish_gpadl(newchannel,
149 newchannel->outbound.ring_buffer,
150 send_ringbuffer_size +
151 recv_ringbuffer_size,
152 &newchannel->ringbuffer_gpadlhandle);
159 /* Create and init the channel open message */
160 open_info = kmalloc(sizeof(*open_info) +
161 sizeof(struct vmbus_channel_open_channel),
168 init_completion(&open_info->waitevent);
170 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
171 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
172 open_msg->openid = newchannel->offermsg.child_relid;
173 open_msg->child_relid = newchannel->offermsg.child_relid;
174 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
175 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
177 open_msg->target_vp = newchannel->target_vp;
179 if (userdatalen > MAX_USER_DEFINED_BYTES) {
185 memcpy(open_msg->userdata, userdata, userdatalen);
187 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
188 list_add_tail(&open_info->msglistentry,
189 &vmbus_connection.chn_msg_list);
190 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
192 ret = vmbus_post_msg(open_msg,
193 sizeof(struct vmbus_channel_open_channel));
198 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
205 if (open_info->response.open_result.status)
206 err = open_info->response.open_result.status;
208 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
209 list_del(&open_info->msglistentry);
210 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
213 newchannel->state = CHANNEL_OPENED_STATE;
219 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
220 list_del(&open_info->msglistentry);
221 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
224 free_pages((unsigned long)out,
225 get_order(send_ringbuffer_size + recv_ringbuffer_size));
229 EXPORT_SYMBOL_GPL(vmbus_open);
232 * create_gpadl_header - Creates a gpadl for the specified buffer
234 static int create_gpadl_header(void *kbuffer, u32 size,
235 struct vmbus_channel_msginfo **msginfo,
240 unsigned long long pfn;
241 struct vmbus_channel_gpadl_header *gpadl_header;
242 struct vmbus_channel_gpadl_body *gpadl_body;
243 struct vmbus_channel_msginfo *msgheader;
244 struct vmbus_channel_msginfo *msgbody = NULL;
247 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
249 pagecount = size >> PAGE_SHIFT;
250 pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
252 /* do we need a gpadl body msg */
253 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
254 sizeof(struct vmbus_channel_gpadl_header) -
255 sizeof(struct gpa_range);
256 pfncount = pfnsize / sizeof(u64);
258 if (pagecount > pfncount) {
259 /* we need a gpadl body */
260 /* fill in the header */
261 msgsize = sizeof(struct vmbus_channel_msginfo) +
262 sizeof(struct vmbus_channel_gpadl_header) +
263 sizeof(struct gpa_range) + pfncount * sizeof(u64);
264 msgheader = kzalloc(msgsize, GFP_KERNEL);
268 INIT_LIST_HEAD(&msgheader->submsglist);
269 msgheader->msgsize = msgsize;
271 gpadl_header = (struct vmbus_channel_gpadl_header *)
273 gpadl_header->rangecount = 1;
274 gpadl_header->range_buflen = sizeof(struct gpa_range) +
275 pagecount * sizeof(u64);
276 gpadl_header->range[0].byte_offset = 0;
277 gpadl_header->range[0].byte_count = size;
278 for (i = 0; i < pfncount; i++)
279 gpadl_header->range[0].pfn_array[i] = pfn+i;
280 *msginfo = msgheader;
284 pfnleft = pagecount - pfncount;
286 /* how many pfns can we fit */
287 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
288 sizeof(struct vmbus_channel_gpadl_body);
289 pfncount = pfnsize / sizeof(u64);
291 /* fill in the body */
293 if (pfnleft > pfncount)
298 msgsize = sizeof(struct vmbus_channel_msginfo) +
299 sizeof(struct vmbus_channel_gpadl_body) +
300 pfncurr * sizeof(u64);
301 msgbody = kzalloc(msgsize, GFP_KERNEL);
304 struct vmbus_channel_msginfo *pos = NULL;
305 struct vmbus_channel_msginfo *tmp = NULL;
307 * Free up all the allocated messages.
309 list_for_each_entry_safe(pos, tmp,
310 &msgheader->submsglist,
313 list_del(&pos->msglistentry);
320 msgbody->msgsize = msgsize;
323 (struct vmbus_channel_gpadl_body *)msgbody->msg;
326 * Gpadl is u32 and we are using a pointer which could
328 * This is governed by the guest/host protocol and
329 * so the hypervisor gurantees that this is ok.
331 for (i = 0; i < pfncurr; i++)
332 gpadl_body->pfn[i] = pfn + pfnsum + i;
334 /* add to msg header */
335 list_add_tail(&msgbody->msglistentry,
336 &msgheader->submsglist);
341 /* everything fits in a header */
342 msgsize = sizeof(struct vmbus_channel_msginfo) +
343 sizeof(struct vmbus_channel_gpadl_header) +
344 sizeof(struct gpa_range) + pagecount * sizeof(u64);
345 msgheader = kzalloc(msgsize, GFP_KERNEL);
346 if (msgheader == NULL)
348 msgheader->msgsize = msgsize;
350 gpadl_header = (struct vmbus_channel_gpadl_header *)
352 gpadl_header->rangecount = 1;
353 gpadl_header->range_buflen = sizeof(struct gpa_range) +
354 pagecount * sizeof(u64);
355 gpadl_header->range[0].byte_offset = 0;
356 gpadl_header->range[0].byte_count = size;
357 for (i = 0; i < pagecount; i++)
358 gpadl_header->range[0].pfn_array[i] = pfn+i;
360 *msginfo = msgheader;
372 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
374 * @channel: a channel
375 * @kbuffer: from kmalloc
376 * @size: page-size multiple
377 * @gpadl_handle: some funky thing
379 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
380 u32 size, u32 *gpadl_handle)
382 struct vmbus_channel_gpadl_header *gpadlmsg;
383 struct vmbus_channel_gpadl_body *gpadl_body;
384 struct vmbus_channel_msginfo *msginfo = NULL;
385 struct vmbus_channel_msginfo *submsginfo;
387 struct list_head *curr;
388 u32 next_gpadl_handle;
393 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
394 atomic_inc(&vmbus_connection.next_gpadl_handle);
396 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
400 init_completion(&msginfo->waitevent);
402 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
403 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
404 gpadlmsg->child_relid = channel->offermsg.child_relid;
405 gpadlmsg->gpadl = next_gpadl_handle;
408 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
409 list_add_tail(&msginfo->msglistentry,
410 &vmbus_connection.chn_msg_list);
412 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
414 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
420 list_for_each(curr, &msginfo->submsglist) {
422 submsginfo = (struct vmbus_channel_msginfo *)curr;
424 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
426 gpadl_body->header.msgtype =
427 CHANNELMSG_GPADL_BODY;
428 gpadl_body->gpadl = next_gpadl_handle;
430 ret = vmbus_post_msg(gpadl_body,
431 submsginfo->msgsize -
432 sizeof(*submsginfo));
438 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
442 /* At this point, we received the gpadl created msg */
443 *gpadl_handle = gpadlmsg->gpadl;
446 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
447 list_del(&msginfo->msglistentry);
448 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
453 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
456 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
458 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
460 struct vmbus_channel_gpadl_teardown *msg;
461 struct vmbus_channel_msginfo *info;
465 info = kmalloc(sizeof(*info) +
466 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
470 init_completion(&info->waitevent);
472 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
474 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
475 msg->child_relid = channel->offermsg.child_relid;
476 msg->gpadl = gpadl_handle;
478 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
479 list_add_tail(&info->msglistentry,
480 &vmbus_connection.chn_msg_list);
481 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
482 ret = vmbus_post_msg(msg,
483 sizeof(struct vmbus_channel_gpadl_teardown));
486 t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
489 /* Received a torndown response */
490 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
491 list_del(&info->msglistentry);
492 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
497 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
499 static void vmbus_close_internal(struct vmbus_channel *channel)
501 struct vmbus_channel_close_channel *msg;
505 channel->state = CHANNEL_OPEN_STATE;
506 channel->sc_creation_callback = NULL;
507 /* Stop callback and cancel the timer asap */
508 spin_lock_irqsave(&channel->inbound_lock, flags);
509 channel->onchannel_callback = NULL;
510 spin_unlock_irqrestore(&channel->inbound_lock, flags);
512 /* Send a closing message */
514 msg = &channel->close_msg.msg;
516 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
517 msg->child_relid = channel->offermsg.child_relid;
519 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
522 /* Tear down the gpadl for the channel's ring buffer */
523 if (channel->ringbuffer_gpadlhandle)
524 vmbus_teardown_gpadl(channel,
525 channel->ringbuffer_gpadlhandle);
527 /* Cleanup the ring buffers for this channel */
528 hv_ringbuffer_cleanup(&channel->outbound);
529 hv_ringbuffer_cleanup(&channel->inbound);
531 free_pages((unsigned long)channel->ringbuffer_pages,
532 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
538 * vmbus_close - Close the specified channel
540 void vmbus_close(struct vmbus_channel *channel)
542 struct list_head *cur, *tmp;
543 struct vmbus_channel *cur_channel;
545 if (channel->primary_channel != NULL) {
547 * We will only close sub-channels when
548 * the primary is closed.
553 * Close all the sub-channels first and then close the
556 list_for_each_safe(cur, tmp, &channel->sc_list) {
557 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
558 if (cur_channel->state != CHANNEL_OPENED_STATE)
560 vmbus_close_internal(cur_channel);
563 * Now close the primary.
565 vmbus_close_internal(channel);
567 EXPORT_SYMBOL_GPL(vmbus_close);
570 * vmbus_sendpacket() - Send the specified buffer on the given channel
571 * @channel: Pointer to vmbus_channel structure.
572 * @buffer: Pointer to the buffer you want to receive the data into.
573 * @bufferlen: Maximum size of what the the buffer will hold
574 * @requestid: Identifier of the request
575 * @type: Type of packet that is being send e.g. negotiate, time
578 * Sends data in @buffer directly to hyper-v via the vmbus
579 * This will send the data unparsed to hyper-v.
581 * Mainly used by Hyper-V drivers.
583 int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
584 u32 bufferlen, u64 requestid,
585 enum vmbus_packet_type type, u32 flags)
587 struct vmpacket_descriptor desc;
588 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
589 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
590 struct scatterlist bufferlist[3];
591 u64 aligned_data = 0;
596 /* Setup the descriptor */
597 desc.type = type; /* VmbusPacketTypeDataInBand; */
598 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
599 /* in 8-bytes granularity */
600 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
601 desc.len8 = (u16)(packetlen_aligned >> 3);
602 desc.trans_id = requestid;
604 sg_init_table(bufferlist, 3);
605 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
606 sg_set_buf(&bufferlist[1], buffer, bufferlen);
607 sg_set_buf(&bufferlist[2], &aligned_data,
608 packetlen_aligned - packetlen);
610 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
612 if (ret == 0 && signal)
613 vmbus_setevent(channel);
617 EXPORT_SYMBOL(vmbus_sendpacket);
620 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
621 * packets using a GPADL Direct packet type.
623 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
624 struct hv_page_buffer pagebuffers[],
625 u32 pagecount, void *buffer, u32 bufferlen,
630 struct vmbus_channel_packet_page_buffer desc;
633 u32 packetlen_aligned;
634 struct scatterlist bufferlist[3];
635 u64 aligned_data = 0;
638 if (pagecount > MAX_PAGE_BUFFER_COUNT)
643 * Adjust the size down since vmbus_channel_packet_page_buffer is the
644 * largest size we support
646 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
647 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
648 sizeof(struct hv_page_buffer));
649 packetlen = descsize + bufferlen;
650 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
652 /* Setup the descriptor */
653 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
654 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
655 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
656 desc.length8 = (u16)(packetlen_aligned >> 3);
657 desc.transactionid = requestid;
658 desc.rangecount = pagecount;
660 for (i = 0; i < pagecount; i++) {
661 desc.range[i].len = pagebuffers[i].len;
662 desc.range[i].offset = pagebuffers[i].offset;
663 desc.range[i].pfn = pagebuffers[i].pfn;
666 sg_init_table(bufferlist, 3);
667 sg_set_buf(&bufferlist[0], &desc, descsize);
668 sg_set_buf(&bufferlist[1], buffer, bufferlen);
669 sg_set_buf(&bufferlist[2], &aligned_data,
670 packetlen_aligned - packetlen);
672 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
674 if (ret == 0 && signal)
675 vmbus_setevent(channel);
679 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
682 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
683 * using a GPADL Direct packet type.
685 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
686 struct hv_multipage_buffer *multi_pagebuffer,
687 void *buffer, u32 bufferlen, u64 requestid)
690 struct vmbus_channel_packet_multipage_buffer desc;
693 u32 packetlen_aligned;
694 struct scatterlist bufferlist[3];
695 u64 aligned_data = 0;
697 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
698 multi_pagebuffer->len);
701 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
705 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
706 * the largest size we support
708 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
709 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
711 packetlen = descsize + bufferlen;
712 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
715 /* Setup the descriptor */
716 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
717 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
718 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
719 desc.length8 = (u16)(packetlen_aligned >> 3);
720 desc.transactionid = requestid;
723 desc.range.len = multi_pagebuffer->len;
724 desc.range.offset = multi_pagebuffer->offset;
726 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
727 pfncount * sizeof(u64));
729 sg_init_table(bufferlist, 3);
730 sg_set_buf(&bufferlist[0], &desc, descsize);
731 sg_set_buf(&bufferlist[1], buffer, bufferlen);
732 sg_set_buf(&bufferlist[2], &aligned_data,
733 packetlen_aligned - packetlen);
735 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
737 if (ret == 0 && signal)
738 vmbus_setevent(channel);
742 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
745 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
746 * @channel: Pointer to vmbus_channel structure.
747 * @buffer: Pointer to the buffer you want to receive the data into.
748 * @bufferlen: Maximum size of what the the buffer will hold
749 * @buffer_actual_len: The actual size of the data after it was received
750 * @requestid: Identifier of the request
752 * Receives directly from the hyper-v vmbus and puts the data it received
753 * into Buffer. This will receive the data unparsed from hyper-v.
755 * Mainly used by Hyper-V drivers.
757 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
758 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
760 struct vmpacket_descriptor desc;
766 *buffer_actual_len = 0;
770 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
771 sizeof(struct vmpacket_descriptor));
775 packetlen = desc.len8 << 3;
776 userlen = packetlen - (desc.offset8 << 3);
778 *buffer_actual_len = userlen;
780 if (userlen > bufferlen) {
782 pr_err("Buffer too small - got %d needs %d\n",
787 *requestid = desc.trans_id;
789 /* Copy over the packet to the user buffer */
790 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
791 (desc.offset8 << 3), &signal);
794 vmbus_setevent(channel);
798 EXPORT_SYMBOL(vmbus_recvpacket);
801 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
803 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
804 u32 bufferlen, u32 *buffer_actual_len,
807 struct vmpacket_descriptor desc;
812 *buffer_actual_len = 0;
816 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
817 sizeof(struct vmpacket_descriptor));
822 packetlen = desc.len8 << 3;
824 *buffer_actual_len = packetlen;
826 if (packetlen > bufferlen) {
827 pr_err("Buffer too small - needed %d bytes but "
828 "got space for only %d bytes\n",
829 packetlen, bufferlen);
833 *requestid = desc.trans_id;
835 /* Copy over the entire packet to the user buffer */
836 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
840 vmbus_setevent(channel);
844 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);