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>
30 #include <linux/uio.h>
31 #include <linux/interrupt.h>
33 #include "hyperv_vmbus.h"
35 #define NUM_PAGES_SPANNED(addr, len) \
36 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
39 * vmbus_setevent- Trigger an event notification on the specified
42 static void vmbus_setevent(struct vmbus_channel *channel)
44 struct hv_monitor_page *monitorpage;
46 if (channel->offermsg.monitor_allocated) {
47 /* Each u32 represents 32 channels */
48 sync_set_bit(channel->offermsg.child_relid & 31,
49 (unsigned long *) vmbus_connection.send_int_page +
50 (channel->offermsg.child_relid >> 5));
52 /* Get the child to parent monitor page */
53 monitorpage = vmbus_connection.monitor_pages[1];
55 sync_set_bit(channel->monitor_bit,
56 (unsigned long *)&monitorpage->trigger_group
57 [channel->monitor_grp].pending);
60 vmbus_set_event(channel);
65 * vmbus_open - Open the specified channel.
67 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
68 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
69 void (*onchannelcallback)(void *context), void *context)
71 struct vmbus_channel_open_channel *open_msg;
72 struct vmbus_channel_msginfo *open_info = NULL;
78 spin_lock_irqsave(&newchannel->lock, flags);
79 if (newchannel->state == CHANNEL_OPEN_STATE) {
80 newchannel->state = CHANNEL_OPENING_STATE;
82 spin_unlock_irqrestore(&newchannel->lock, flags);
85 spin_unlock_irqrestore(&newchannel->lock, flags);
87 newchannel->onchannel_callback = onchannelcallback;
88 newchannel->channel_callback_context = context;
90 /* Allocate the ring buffer */
91 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
92 GFP_KERNEL|__GFP_ZERO,
93 get_order(send_ringbuffer_size +
94 recv_ringbuffer_size));
97 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
98 get_order(send_ringbuffer_size +
99 recv_ringbuffer_size));
101 out = (void *)page_address(page);
108 in = (void *)((unsigned long)out + send_ringbuffer_size);
110 newchannel->ringbuffer_pages = out;
111 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
112 recv_ringbuffer_size) >> PAGE_SHIFT;
114 ret = hv_ringbuffer_init(
115 &newchannel->outbound, out, send_ringbuffer_size);
122 ret = hv_ringbuffer_init(
123 &newchannel->inbound, in, recv_ringbuffer_size);
130 /* Establish the gpadl for the ring buffer */
131 newchannel->ringbuffer_gpadlhandle = 0;
133 ret = vmbus_establish_gpadl(newchannel,
134 newchannel->outbound.ring_buffer,
135 send_ringbuffer_size +
136 recv_ringbuffer_size,
137 &newchannel->ringbuffer_gpadlhandle);
144 /* Create and init the channel open message */
145 open_info = kmalloc(sizeof(*open_info) +
146 sizeof(struct vmbus_channel_open_channel),
153 init_completion(&open_info->waitevent);
155 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
156 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
157 open_msg->openid = newchannel->offermsg.child_relid;
158 open_msg->child_relid = newchannel->offermsg.child_relid;
159 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
160 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
162 open_msg->target_vp = newchannel->target_vp;
164 if (userdatalen > MAX_USER_DEFINED_BYTES) {
170 memcpy(open_msg->userdata, userdata, userdatalen);
172 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
173 list_add_tail(&open_info->msglistentry,
174 &vmbus_connection.chn_msg_list);
175 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
177 ret = vmbus_post_msg(open_msg,
178 sizeof(struct vmbus_channel_open_channel));
185 wait_for_completion(&open_info->waitevent);
187 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
188 list_del(&open_info->msglistentry);
189 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
191 if (open_info->response.open_result.status) {
196 newchannel->state = CHANNEL_OPENED_STATE;
201 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
202 list_del(&open_info->msglistentry);
203 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
206 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
209 free_pages((unsigned long)out,
210 get_order(send_ringbuffer_size + recv_ringbuffer_size));
212 newchannel->state = CHANNEL_OPEN_STATE;
215 EXPORT_SYMBOL_GPL(vmbus_open);
218 * create_gpadl_header - Creates a gpadl for the specified buffer
220 static int create_gpadl_header(void *kbuffer, u32 size,
221 struct vmbus_channel_msginfo **msginfo,
226 struct vmbus_channel_gpadl_header *gpadl_header;
227 struct vmbus_channel_gpadl_body *gpadl_body;
228 struct vmbus_channel_msginfo *msgheader;
229 struct vmbus_channel_msginfo *msgbody = NULL;
232 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
234 pagecount = size >> PAGE_SHIFT;
236 /* do we need a gpadl body msg */
237 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
238 sizeof(struct vmbus_channel_gpadl_header) -
239 sizeof(struct gpa_range);
240 pfncount = pfnsize / sizeof(u64);
242 if (pagecount > pfncount) {
243 /* we need a gpadl body */
244 /* fill in the header */
245 msgsize = sizeof(struct vmbus_channel_msginfo) +
246 sizeof(struct vmbus_channel_gpadl_header) +
247 sizeof(struct gpa_range) + pfncount * sizeof(u64);
248 msgheader = kzalloc(msgsize, GFP_KERNEL);
252 INIT_LIST_HEAD(&msgheader->submsglist);
253 msgheader->msgsize = msgsize;
255 gpadl_header = (struct vmbus_channel_gpadl_header *)
257 gpadl_header->rangecount = 1;
258 gpadl_header->range_buflen = sizeof(struct gpa_range) +
259 pagecount * sizeof(u64);
260 gpadl_header->range[0].byte_offset = 0;
261 gpadl_header->range[0].byte_count = size;
262 for (i = 0; i < pfncount; i++)
263 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
264 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
265 *msginfo = msgheader;
269 pfnleft = pagecount - pfncount;
271 /* how many pfns can we fit */
272 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
273 sizeof(struct vmbus_channel_gpadl_body);
274 pfncount = pfnsize / sizeof(u64);
276 /* fill in the body */
278 if (pfnleft > pfncount)
283 msgsize = sizeof(struct vmbus_channel_msginfo) +
284 sizeof(struct vmbus_channel_gpadl_body) +
285 pfncurr * sizeof(u64);
286 msgbody = kzalloc(msgsize, GFP_KERNEL);
289 struct vmbus_channel_msginfo *pos = NULL;
290 struct vmbus_channel_msginfo *tmp = NULL;
292 * Free up all the allocated messages.
294 list_for_each_entry_safe(pos, tmp,
295 &msgheader->submsglist,
298 list_del(&pos->msglistentry);
305 msgbody->msgsize = msgsize;
308 (struct vmbus_channel_gpadl_body *)msgbody->msg;
311 * Gpadl is u32 and we are using a pointer which could
313 * This is governed by the guest/host protocol and
314 * so the hypervisor gurantees that this is ok.
316 for (i = 0; i < pfncurr; i++)
317 gpadl_body->pfn[i] = slow_virt_to_phys(
318 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
321 /* add to msg header */
322 list_add_tail(&msgbody->msglistentry,
323 &msgheader->submsglist);
328 /* everything fits in a header */
329 msgsize = sizeof(struct vmbus_channel_msginfo) +
330 sizeof(struct vmbus_channel_gpadl_header) +
331 sizeof(struct gpa_range) + pagecount * sizeof(u64);
332 msgheader = kzalloc(msgsize, GFP_KERNEL);
333 if (msgheader == NULL)
335 msgheader->msgsize = msgsize;
337 gpadl_header = (struct vmbus_channel_gpadl_header *)
339 gpadl_header->rangecount = 1;
340 gpadl_header->range_buflen = sizeof(struct gpa_range) +
341 pagecount * sizeof(u64);
342 gpadl_header->range[0].byte_offset = 0;
343 gpadl_header->range[0].byte_count = size;
344 for (i = 0; i < pagecount; i++)
345 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
346 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
348 *msginfo = msgheader;
360 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
362 * @channel: a channel
363 * @kbuffer: from kmalloc or vmalloc
364 * @size: page-size multiple
365 * @gpadl_handle: some funky thing
367 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
368 u32 size, u32 *gpadl_handle)
370 struct vmbus_channel_gpadl_header *gpadlmsg;
371 struct vmbus_channel_gpadl_body *gpadl_body;
372 struct vmbus_channel_msginfo *msginfo = NULL;
373 struct vmbus_channel_msginfo *submsginfo, *tmp;
375 struct list_head *curr;
376 u32 next_gpadl_handle;
381 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
383 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
387 init_completion(&msginfo->waitevent);
389 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
390 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
391 gpadlmsg->child_relid = channel->offermsg.child_relid;
392 gpadlmsg->gpadl = next_gpadl_handle;
395 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
396 list_add_tail(&msginfo->msglistentry,
397 &vmbus_connection.chn_msg_list);
399 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
401 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
407 list_for_each(curr, &msginfo->submsglist) {
409 submsginfo = (struct vmbus_channel_msginfo *)curr;
411 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
413 gpadl_body->header.msgtype =
414 CHANNELMSG_GPADL_BODY;
415 gpadl_body->gpadl = next_gpadl_handle;
417 ret = vmbus_post_msg(gpadl_body,
418 submsginfo->msgsize -
419 sizeof(*submsginfo));
425 wait_for_completion(&msginfo->waitevent);
427 /* At this point, we received the gpadl created msg */
428 *gpadl_handle = gpadlmsg->gpadl;
431 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
432 list_del(&msginfo->msglistentry);
433 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
436 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
445 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
448 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
450 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
452 struct vmbus_channel_gpadl_teardown *msg;
453 struct vmbus_channel_msginfo *info;
457 info = kmalloc(sizeof(*info) +
458 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
462 init_completion(&info->waitevent);
464 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
466 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
467 msg->child_relid = channel->offermsg.child_relid;
468 msg->gpadl = gpadl_handle;
470 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
471 list_add_tail(&info->msglistentry,
472 &vmbus_connection.chn_msg_list);
473 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
474 ret = vmbus_post_msg(msg,
475 sizeof(struct vmbus_channel_gpadl_teardown));
480 wait_for_completion(&info->waitevent);
483 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
484 list_del(&info->msglistentry);
485 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
490 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
492 static void reset_channel_cb(void *arg)
494 struct vmbus_channel *channel = arg;
496 channel->onchannel_callback = NULL;
499 static int vmbus_close_internal(struct vmbus_channel *channel)
501 struct vmbus_channel_close_channel *msg;
502 struct tasklet_struct *tasklet;
506 * process_chn_event(), running in the tasklet, can race
507 * with vmbus_close_internal() in the case of SMP guest, e.g., when
508 * the former is accessing channel->inbound.ring_buffer, the latter
509 * could be freeing the ring_buffer pages.
511 * To resolve the race, we can serialize them by disabling the
512 * tasklet when the latter is running here.
514 tasklet = hv_context.event_dpc[channel->target_cpu];
515 tasklet_disable(tasklet);
517 channel->state = CHANNEL_OPEN_STATE;
518 channel->sc_creation_callback = NULL;
519 /* Stop callback and cancel the timer asap */
520 if (channel->target_cpu != get_cpu()) {
522 smp_call_function_single(channel->target_cpu, reset_channel_cb,
525 reset_channel_cb(channel);
529 /* Send a closing message */
531 msg = &channel->close_msg.msg;
533 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
534 msg->child_relid = channel->offermsg.child_relid;
536 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
539 pr_err("Close failed: close post msg return is %d\n", ret);
541 * If we failed to post the close msg,
542 * it is perhaps better to leak memory.
547 /* Tear down the gpadl for the channel's ring buffer */
548 if (channel->ringbuffer_gpadlhandle) {
549 ret = vmbus_teardown_gpadl(channel,
550 channel->ringbuffer_gpadlhandle);
552 pr_err("Close failed: teardown gpadl return %d\n", ret);
554 * If we failed to teardown gpadl,
555 * it is perhaps better to leak memory.
561 /* Cleanup the ring buffers for this channel */
562 hv_ringbuffer_cleanup(&channel->outbound);
563 hv_ringbuffer_cleanup(&channel->inbound);
565 free_pages((unsigned long)channel->ringbuffer_pages,
566 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
569 tasklet_enable(tasklet);
575 * vmbus_close - Close the specified channel
577 void vmbus_close(struct vmbus_channel *channel)
579 struct list_head *cur, *tmp;
580 struct vmbus_channel *cur_channel;
582 if (channel->primary_channel != NULL) {
584 * We will only close sub-channels when
585 * the primary is closed.
590 * Close all the sub-channels first and then close the
593 list_for_each_safe(cur, tmp, &channel->sc_list) {
594 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
595 if (cur_channel->state != CHANNEL_OPENED_STATE)
597 vmbus_close_internal(cur_channel);
600 * Now close the primary.
602 vmbus_close_internal(channel);
604 EXPORT_SYMBOL_GPL(vmbus_close);
606 int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
607 u32 bufferlen, u64 requestid,
608 enum vmbus_packet_type type, u32 flags, bool kick_q)
610 struct vmpacket_descriptor desc;
611 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
612 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
613 struct kvec bufferlist[3];
614 u64 aligned_data = 0;
617 int num_vecs = ((bufferlen != 0) ? 3 : 1);
620 /* Setup the descriptor */
621 desc.type = type; /* VmbusPacketTypeDataInBand; */
622 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
623 /* in 8-bytes granularity */
624 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
625 desc.len8 = (u16)(packetlen_aligned >> 3);
626 desc.trans_id = requestid;
628 bufferlist[0].iov_base = &desc;
629 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
630 bufferlist[1].iov_base = buffer;
631 bufferlist[1].iov_len = bufferlen;
632 bufferlist[2].iov_base = &aligned_data;
633 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
635 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, num_vecs,
639 * Signalling the host is conditional on many factors:
640 * 1. The ring state changed from being empty to non-empty.
641 * This is tracked by the variable "signal".
642 * 2. The variable kick_q tracks if more data will be placed
643 * on the ring. We will not signal if more data is
646 * Based on the channel signal state, we will decide
647 * which signaling policy will be applied.
649 * If we cannot write to the ring-buffer; signal the host
650 * even if we may not have written anything. This is a rare
651 * enough condition that it should not matter.
654 if (channel->signal_policy)
659 if (((ret == 0) && kick_q && signal) || (ret))
660 vmbus_setevent(channel);
664 EXPORT_SYMBOL(vmbus_sendpacket_ctl);
667 * vmbus_sendpacket() - Send the specified buffer on the given channel
668 * @channel: Pointer to vmbus_channel structure.
669 * @buffer: Pointer to the buffer you want to receive the data into.
670 * @bufferlen: Maximum size of what the the buffer will hold
671 * @requestid: Identifier of the request
672 * @type: Type of packet that is being send e.g. negotiate, time
675 * Sends data in @buffer directly to hyper-v via the vmbus
676 * This will send the data unparsed to hyper-v.
678 * Mainly used by Hyper-V drivers.
680 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
681 u32 bufferlen, u64 requestid,
682 enum vmbus_packet_type type, u32 flags)
684 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
687 EXPORT_SYMBOL(vmbus_sendpacket);
690 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
691 * packets using a GPADL Direct packet type. This interface allows you
692 * to control notifying the host. This will be useful for sending
693 * batched data. Also the sender can control the send flags
696 int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
697 struct hv_page_buffer pagebuffers[],
698 u32 pagecount, void *buffer, u32 bufferlen,
705 struct vmbus_channel_packet_page_buffer desc;
708 u32 packetlen_aligned;
709 struct kvec bufferlist[3];
710 u64 aligned_data = 0;
713 if (pagecount > MAX_PAGE_BUFFER_COUNT)
718 * Adjust the size down since vmbus_channel_packet_page_buffer is the
719 * largest size we support
721 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
722 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
723 sizeof(struct hv_page_buffer));
724 packetlen = descsize + bufferlen;
725 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
727 /* Setup the descriptor */
728 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
730 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
731 desc.length8 = (u16)(packetlen_aligned >> 3);
732 desc.transactionid = requestid;
733 desc.rangecount = pagecount;
735 for (i = 0; i < pagecount; i++) {
736 desc.range[i].len = pagebuffers[i].len;
737 desc.range[i].offset = pagebuffers[i].offset;
738 desc.range[i].pfn = pagebuffers[i].pfn;
741 bufferlist[0].iov_base = &desc;
742 bufferlist[0].iov_len = descsize;
743 bufferlist[1].iov_base = buffer;
744 bufferlist[1].iov_len = bufferlen;
745 bufferlist[2].iov_base = &aligned_data;
746 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
748 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
751 * Signalling the host is conditional on many factors:
752 * 1. The ring state changed from being empty to non-empty.
753 * This is tracked by the variable "signal".
754 * 2. The variable kick_q tracks if more data will be placed
755 * on the ring. We will not signal if more data is
758 * Based on the channel signal state, we will decide
759 * which signaling policy will be applied.
761 * If we cannot write to the ring-buffer; signal the host
762 * even if we may not have written anything. This is a rare
763 * enough condition that it should not matter.
766 if (channel->signal_policy)
771 if (((ret == 0) && kick_q && signal) || (ret))
772 vmbus_setevent(channel);
776 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
779 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
780 * packets using a GPADL Direct packet type.
782 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
783 struct hv_page_buffer pagebuffers[],
784 u32 pagecount, void *buffer, u32 bufferlen,
787 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
788 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
789 buffer, bufferlen, requestid,
793 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
796 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
797 * using a GPADL Direct packet type.
798 * The buffer includes the vmbus descriptor.
800 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
801 struct vmbus_packet_mpb_array *desc,
803 void *buffer, u32 bufferlen, u64 requestid)
807 u32 packetlen_aligned;
808 struct kvec bufferlist[3];
809 u64 aligned_data = 0;
812 packetlen = desc_size + bufferlen;
813 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
815 /* Setup the descriptor */
816 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
817 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
818 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
819 desc->length8 = (u16)(packetlen_aligned >> 3);
820 desc->transactionid = requestid;
821 desc->rangecount = 1;
823 bufferlist[0].iov_base = desc;
824 bufferlist[0].iov_len = desc_size;
825 bufferlist[1].iov_base = buffer;
826 bufferlist[1].iov_len = bufferlen;
827 bufferlist[2].iov_base = &aligned_data;
828 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
830 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
832 if (ret == 0 && signal)
833 vmbus_setevent(channel);
837 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
840 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
841 * using a GPADL Direct packet type.
843 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
844 struct hv_multipage_buffer *multi_pagebuffer,
845 void *buffer, u32 bufferlen, u64 requestid)
848 struct vmbus_channel_packet_multipage_buffer desc;
851 u32 packetlen_aligned;
852 struct kvec bufferlist[3];
853 u64 aligned_data = 0;
855 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
856 multi_pagebuffer->len);
858 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
862 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
863 * the largest size we support
865 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
866 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
868 packetlen = descsize + bufferlen;
869 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
872 /* Setup the descriptor */
873 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
874 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
875 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
876 desc.length8 = (u16)(packetlen_aligned >> 3);
877 desc.transactionid = requestid;
880 desc.range.len = multi_pagebuffer->len;
881 desc.range.offset = multi_pagebuffer->offset;
883 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
884 pfncount * sizeof(u64));
886 bufferlist[0].iov_base = &desc;
887 bufferlist[0].iov_len = descsize;
888 bufferlist[1].iov_base = buffer;
889 bufferlist[1].iov_len = bufferlen;
890 bufferlist[2].iov_base = &aligned_data;
891 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
893 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
895 if (ret == 0 && signal)
896 vmbus_setevent(channel);
900 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
903 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
904 * @channel: Pointer to vmbus_channel structure.
905 * @buffer: Pointer to the buffer you want to receive the data into.
906 * @bufferlen: Maximum size of what the the buffer will hold
907 * @buffer_actual_len: The actual size of the data after it was received
908 * @requestid: Identifier of the request
910 * Receives directly from the hyper-v vmbus and puts the data it received
911 * into Buffer. This will receive the data unparsed from hyper-v.
913 * Mainly used by Hyper-V drivers.
915 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
916 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
918 struct vmpacket_descriptor desc;
924 *buffer_actual_len = 0;
928 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
929 sizeof(struct vmpacket_descriptor));
933 packetlen = desc.len8 << 3;
934 userlen = packetlen - (desc.offset8 << 3);
936 *buffer_actual_len = userlen;
938 if (userlen > bufferlen) {
940 pr_err("Buffer too small - got %d needs %d\n",
945 *requestid = desc.trans_id;
947 /* Copy over the packet to the user buffer */
948 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
949 (desc.offset8 << 3), &signal);
952 vmbus_setevent(channel);
956 EXPORT_SYMBOL(vmbus_recvpacket);
959 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
961 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
962 u32 bufferlen, u32 *buffer_actual_len,
965 struct vmpacket_descriptor desc;
970 *buffer_actual_len = 0;
974 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
975 sizeof(struct vmpacket_descriptor));
980 packetlen = desc.len8 << 3;
982 *buffer_actual_len = packetlen;
984 if (packetlen > bufferlen)
987 *requestid = desc.trans_id;
989 /* Copy over the entire packet to the user buffer */
990 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
994 vmbus_setevent(channel);
998 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);