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 void vmbus_setevent(struct vmbus_channel *channel)
44 struct hv_monitor_page *monitorpage;
47 * For channels marked as in "low latency" mode
48 * bypass the monitor page mechanism.
50 if (channel->offermsg.monitor_allocated && !channel->low_latency) {
51 vmbus_send_interrupt(channel->offermsg.child_relid);
53 /* Get the child to parent monitor page */
54 monitorpage = vmbus_connection.monitor_pages[1];
56 sync_set_bit(channel->monitor_bit,
57 (unsigned long *)&monitorpage->trigger_group
58 [channel->monitor_grp].pending);
61 vmbus_set_event(channel);
64 EXPORT_SYMBOL_GPL(vmbus_setevent);
67 * vmbus_open - Open the specified channel.
69 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
70 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
71 void (*onchannelcallback)(void *context), void *context)
73 struct vmbus_channel_open_channel *open_msg;
74 struct vmbus_channel_msginfo *open_info = NULL;
79 if (send_ringbuffer_size % PAGE_SIZE ||
80 recv_ringbuffer_size % PAGE_SIZE)
83 spin_lock_irqsave(&newchannel->lock, flags);
84 if (newchannel->state == CHANNEL_OPEN_STATE) {
85 newchannel->state = CHANNEL_OPENING_STATE;
87 spin_unlock_irqrestore(&newchannel->lock, flags);
90 spin_unlock_irqrestore(&newchannel->lock, flags);
92 newchannel->onchannel_callback = onchannelcallback;
93 newchannel->channel_callback_context = context;
95 /* Allocate the ring buffer */
96 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
97 GFP_KERNEL|__GFP_ZERO,
98 get_order(send_ringbuffer_size +
99 recv_ringbuffer_size));
102 page = alloc_pages(GFP_KERNEL|__GFP_ZERO,
103 get_order(send_ringbuffer_size +
104 recv_ringbuffer_size));
108 goto error_set_chnstate;
111 newchannel->ringbuffer_pages = page_address(page);
112 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
113 recv_ringbuffer_size) >> PAGE_SHIFT;
115 ret = hv_ringbuffer_init(&newchannel->outbound, page,
116 send_ringbuffer_size >> PAGE_SHIFT);
120 goto error_free_pages;
123 ret = hv_ringbuffer_init(&newchannel->inbound,
124 &page[send_ringbuffer_size >> PAGE_SHIFT],
125 recv_ringbuffer_size >> PAGE_SHIFT);
128 goto error_free_pages;
132 /* Establish the gpadl for the ring buffer */
133 newchannel->ringbuffer_gpadlhandle = 0;
135 ret = vmbus_establish_gpadl(newchannel,
137 send_ringbuffer_size +
138 recv_ringbuffer_size,
139 &newchannel->ringbuffer_gpadlhandle);
143 goto error_free_pages;
146 /* Create and init the channel open message */
147 open_info = kmalloc(sizeof(*open_info) +
148 sizeof(struct vmbus_channel_open_channel),
152 goto error_free_gpadl;
155 init_completion(&open_info->waitevent);
156 open_info->waiting_channel = newchannel;
158 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
159 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
160 open_msg->openid = newchannel->offermsg.child_relid;
161 open_msg->child_relid = newchannel->offermsg.child_relid;
162 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
163 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
165 open_msg->target_vp = newchannel->target_vp;
167 if (userdatalen > MAX_USER_DEFINED_BYTES) {
169 goto error_free_gpadl;
173 memcpy(open_msg->userdata, userdata, userdatalen);
175 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
176 list_add_tail(&open_info->msglistentry,
177 &vmbus_connection.chn_msg_list);
178 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
180 if (newchannel->rescind) {
182 goto error_free_gpadl;
185 ret = vmbus_post_msg(open_msg,
186 sizeof(struct vmbus_channel_open_channel), true);
190 goto error_clean_msglist;
193 wait_for_completion(&open_info->waitevent);
195 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
196 list_del(&open_info->msglistentry);
197 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
199 if (newchannel->rescind) {
201 goto error_free_gpadl;
204 if (open_info->response.open_result.status) {
206 goto error_free_gpadl;
209 newchannel->state = CHANNEL_OPENED_STATE;
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 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
222 hv_ringbuffer_cleanup(&newchannel->outbound);
223 hv_ringbuffer_cleanup(&newchannel->inbound);
225 get_order(send_ringbuffer_size + recv_ringbuffer_size));
227 newchannel->state = CHANNEL_OPEN_STATE;
230 EXPORT_SYMBOL_GPL(vmbus_open);
232 /* Used for Hyper-V Socket: a guest client's connect() to the host */
233 int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
234 const uuid_le *shv_host_servie_id)
236 struct vmbus_channel_tl_connect_request conn_msg;
238 memset(&conn_msg, 0, sizeof(conn_msg));
239 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
240 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
241 conn_msg.host_service_id = *shv_host_servie_id;
243 return vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
245 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
248 * create_gpadl_header - Creates a gpadl for the specified buffer
250 static int create_gpadl_header(void *kbuffer, u32 size,
251 struct vmbus_channel_msginfo **msginfo)
255 struct vmbus_channel_gpadl_header *gpadl_header;
256 struct vmbus_channel_gpadl_body *gpadl_body;
257 struct vmbus_channel_msginfo *msgheader;
258 struct vmbus_channel_msginfo *msgbody = NULL;
261 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
263 pagecount = size >> PAGE_SHIFT;
265 /* do we need a gpadl body msg */
266 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
267 sizeof(struct vmbus_channel_gpadl_header) -
268 sizeof(struct gpa_range);
269 pfncount = pfnsize / sizeof(u64);
271 if (pagecount > pfncount) {
272 /* we need a gpadl body */
273 /* fill in the header */
274 msgsize = sizeof(struct vmbus_channel_msginfo) +
275 sizeof(struct vmbus_channel_gpadl_header) +
276 sizeof(struct gpa_range) + pfncount * sizeof(u64);
277 msgheader = kzalloc(msgsize, GFP_KERNEL);
281 INIT_LIST_HEAD(&msgheader->submsglist);
282 msgheader->msgsize = msgsize;
284 gpadl_header = (struct vmbus_channel_gpadl_header *)
286 gpadl_header->rangecount = 1;
287 gpadl_header->range_buflen = sizeof(struct gpa_range) +
288 pagecount * sizeof(u64);
289 gpadl_header->range[0].byte_offset = 0;
290 gpadl_header->range[0].byte_count = size;
291 for (i = 0; i < pfncount; i++)
292 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
293 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
294 *msginfo = msgheader;
297 pfnleft = pagecount - pfncount;
299 /* how many pfns can we fit */
300 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
301 sizeof(struct vmbus_channel_gpadl_body);
302 pfncount = pfnsize / sizeof(u64);
304 /* fill in the body */
306 if (pfnleft > pfncount)
311 msgsize = sizeof(struct vmbus_channel_msginfo) +
312 sizeof(struct vmbus_channel_gpadl_body) +
313 pfncurr * sizeof(u64);
314 msgbody = kzalloc(msgsize, GFP_KERNEL);
317 struct vmbus_channel_msginfo *pos = NULL;
318 struct vmbus_channel_msginfo *tmp = NULL;
320 * Free up all the allocated messages.
322 list_for_each_entry_safe(pos, tmp,
323 &msgheader->submsglist,
326 list_del(&pos->msglistentry);
333 msgbody->msgsize = msgsize;
335 (struct vmbus_channel_gpadl_body *)msgbody->msg;
338 * Gpadl is u32 and we are using a pointer which could
340 * This is governed by the guest/host protocol and
341 * so the hypervisor guarantees that this is ok.
343 for (i = 0; i < pfncurr; i++)
344 gpadl_body->pfn[i] = slow_virt_to_phys(
345 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
348 /* add to msg header */
349 list_add_tail(&msgbody->msglistentry,
350 &msgheader->submsglist);
355 /* everything fits in a header */
356 msgsize = sizeof(struct vmbus_channel_msginfo) +
357 sizeof(struct vmbus_channel_gpadl_header) +
358 sizeof(struct gpa_range) + pagecount * sizeof(u64);
359 msgheader = kzalloc(msgsize, GFP_KERNEL);
360 if (msgheader == NULL)
363 INIT_LIST_HEAD(&msgheader->submsglist);
364 msgheader->msgsize = msgsize;
366 gpadl_header = (struct vmbus_channel_gpadl_header *)
368 gpadl_header->rangecount = 1;
369 gpadl_header->range_buflen = sizeof(struct gpa_range) +
370 pagecount * sizeof(u64);
371 gpadl_header->range[0].byte_offset = 0;
372 gpadl_header->range[0].byte_count = size;
373 for (i = 0; i < pagecount; i++)
374 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
375 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
377 *msginfo = msgheader;
388 * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
390 * @channel: a channel
391 * @kbuffer: from kmalloc or vmalloc
392 * @size: page-size multiple
393 * @gpadl_handle: some funky thing
395 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
396 u32 size, u32 *gpadl_handle)
398 struct vmbus_channel_gpadl_header *gpadlmsg;
399 struct vmbus_channel_gpadl_body *gpadl_body;
400 struct vmbus_channel_msginfo *msginfo = NULL;
401 struct vmbus_channel_msginfo *submsginfo, *tmp;
402 struct list_head *curr;
403 u32 next_gpadl_handle;
408 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
410 ret = create_gpadl_header(kbuffer, size, &msginfo);
414 init_completion(&msginfo->waitevent);
415 msginfo->waiting_channel = channel;
417 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
418 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
419 gpadlmsg->child_relid = channel->offermsg.child_relid;
420 gpadlmsg->gpadl = next_gpadl_handle;
423 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
424 list_add_tail(&msginfo->msglistentry,
425 &vmbus_connection.chn_msg_list);
427 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
429 if (channel->rescind) {
434 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
435 sizeof(*msginfo), true);
439 list_for_each(curr, &msginfo->submsglist) {
440 submsginfo = (struct vmbus_channel_msginfo *)curr;
442 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
444 gpadl_body->header.msgtype =
445 CHANNELMSG_GPADL_BODY;
446 gpadl_body->gpadl = next_gpadl_handle;
448 ret = vmbus_post_msg(gpadl_body,
449 submsginfo->msgsize - sizeof(*submsginfo),
455 wait_for_completion(&msginfo->waitevent);
457 if (msginfo->response.gpadl_created.creation_status != 0) {
458 pr_err("Failed to establish GPADL: err = 0x%x\n",
459 msginfo->response.gpadl_created.creation_status);
465 if (channel->rescind) {
470 /* At this point, we received the gpadl created msg */
471 *gpadl_handle = gpadlmsg->gpadl;
474 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
475 list_del(&msginfo->msglistentry);
476 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
477 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
485 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
488 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
490 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
492 struct vmbus_channel_gpadl_teardown *msg;
493 struct vmbus_channel_msginfo *info;
497 info = kmalloc(sizeof(*info) +
498 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
502 init_completion(&info->waitevent);
503 info->waiting_channel = channel;
505 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
507 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
508 msg->child_relid = channel->offermsg.child_relid;
509 msg->gpadl = gpadl_handle;
511 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
512 list_add_tail(&info->msglistentry,
513 &vmbus_connection.chn_msg_list);
514 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
516 if (channel->rescind)
519 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
525 wait_for_completion(&info->waitevent);
529 * If the channel has been rescinded;
530 * we will be awakened by the rescind
531 * handler; set the error code to zero so we don't leak memory.
533 if (channel->rescind)
536 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
537 list_del(&info->msglistentry);
538 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
543 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
545 static void reset_channel_cb(void *arg)
547 struct vmbus_channel *channel = arg;
549 channel->onchannel_callback = NULL;
552 void vmbus_reset_channel_cb(struct vmbus_channel *channel)
555 * vmbus_on_event(), running in the per-channel tasklet, can race
556 * with vmbus_close_internal() in the case of SMP guest, e.g., when
557 * the former is accessing channel->inbound.ring_buffer, the latter
558 * could be freeing the ring_buffer pages, so here we must stop it
561 tasklet_disable(&channel->callback_event);
563 channel->sc_creation_callback = NULL;
565 /* Stop the callback asap */
566 if (channel->target_cpu != get_cpu()) {
568 smp_call_function_single(channel->target_cpu, reset_channel_cb,
571 reset_channel_cb(channel);
575 /* Re-enable tasklet for use on re-open */
576 tasklet_enable(&channel->callback_event);
579 static int vmbus_close_internal(struct vmbus_channel *channel)
581 struct vmbus_channel_close_channel *msg;
584 vmbus_reset_channel_cb(channel);
587 * In case a device driver's probe() fails (e.g.,
588 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
589 * rescinded later (e.g., we dynamically disable an Integrated Service
590 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
591 * here we should skip most of the below cleanup work.
593 if (channel->state != CHANNEL_OPENED_STATE) {
598 channel->state = CHANNEL_OPEN_STATE;
600 /* Send a closing message */
602 msg = &channel->close_msg.msg;
604 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
605 msg->child_relid = channel->offermsg.child_relid;
607 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
611 pr_err("Close failed: close post msg return is %d\n", ret);
613 * If we failed to post the close msg,
614 * it is perhaps better to leak memory.
619 /* Tear down the gpadl for the channel's ring buffer */
620 if (channel->ringbuffer_gpadlhandle) {
621 ret = vmbus_teardown_gpadl(channel,
622 channel->ringbuffer_gpadlhandle);
624 pr_err("Close failed: teardown gpadl return %d\n", ret);
626 * If we failed to teardown gpadl,
627 * it is perhaps better to leak memory.
633 /* Cleanup the ring buffers for this channel */
634 hv_ringbuffer_cleanup(&channel->outbound);
635 hv_ringbuffer_cleanup(&channel->inbound);
637 free_pages((unsigned long)channel->ringbuffer_pages,
638 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
645 * vmbus_close - Close the specified channel
647 void vmbus_close(struct vmbus_channel *channel)
649 struct list_head *cur, *tmp;
650 struct vmbus_channel *cur_channel;
652 if (channel->primary_channel != NULL) {
654 * We will only close sub-channels when
655 * the primary is closed.
660 * Close all the sub-channels first and then close the
663 list_for_each_safe(cur, tmp, &channel->sc_list) {
664 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
665 if (cur_channel->rescind) {
666 wait_for_completion(&cur_channel->rescind_event);
667 mutex_lock(&vmbus_connection.channel_mutex);
668 vmbus_close_internal(cur_channel);
669 hv_process_channel_removal(
670 cur_channel->offermsg.child_relid);
672 mutex_lock(&vmbus_connection.channel_mutex);
673 vmbus_close_internal(cur_channel);
675 mutex_unlock(&vmbus_connection.channel_mutex);
678 * Now close the primary.
680 mutex_lock(&vmbus_connection.channel_mutex);
681 vmbus_close_internal(channel);
682 mutex_unlock(&vmbus_connection.channel_mutex);
684 EXPORT_SYMBOL_GPL(vmbus_close);
687 * vmbus_sendpacket() - Send the specified buffer on the given channel
688 * @channel: Pointer to vmbus_channel structure.
689 * @buffer: Pointer to the buffer you want to receive the data into.
690 * @bufferlen: Maximum size of what the the buffer will hold
691 * @requestid: Identifier of the request
692 * @type: Type of packet that is being send e.g. negotiate, time
695 * Sends data in @buffer directly to hyper-v via the vmbus
696 * This will send the data unparsed to hyper-v.
698 * Mainly used by Hyper-V drivers.
700 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
701 u32 bufferlen, u64 requestid,
702 enum vmbus_packet_type type, u32 flags)
704 struct vmpacket_descriptor desc;
705 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
706 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
707 struct kvec bufferlist[3];
708 u64 aligned_data = 0;
709 int num_vecs = ((bufferlen != 0) ? 3 : 1);
712 /* Setup the descriptor */
713 desc.type = type; /* VmbusPacketTypeDataInBand; */
714 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
715 /* in 8-bytes granularity */
716 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
717 desc.len8 = (u16)(packetlen_aligned >> 3);
718 desc.trans_id = requestid;
720 bufferlist[0].iov_base = &desc;
721 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
722 bufferlist[1].iov_base = buffer;
723 bufferlist[1].iov_len = bufferlen;
724 bufferlist[2].iov_base = &aligned_data;
725 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
727 return hv_ringbuffer_write(channel, bufferlist, num_vecs);
729 EXPORT_SYMBOL(vmbus_sendpacket);
732 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
733 * packets using a GPADL Direct packet type. This interface allows you
734 * to control notifying the host. This will be useful for sending
735 * batched data. Also the sender can control the send flags
738 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
739 struct hv_page_buffer pagebuffers[],
740 u32 pagecount, void *buffer, u32 bufferlen,
744 struct vmbus_channel_packet_page_buffer desc;
747 u32 packetlen_aligned;
748 struct kvec bufferlist[3];
749 u64 aligned_data = 0;
751 if (pagecount > MAX_PAGE_BUFFER_COUNT)
755 * Adjust the size down since vmbus_channel_packet_page_buffer is the
756 * largest size we support
758 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
759 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
760 sizeof(struct hv_page_buffer));
761 packetlen = descsize + bufferlen;
762 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
764 /* Setup the descriptor */
765 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
766 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
767 desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
768 desc.length8 = (u16)(packetlen_aligned >> 3);
769 desc.transactionid = requestid;
770 desc.rangecount = pagecount;
772 for (i = 0; i < pagecount; i++) {
773 desc.range[i].len = pagebuffers[i].len;
774 desc.range[i].offset = pagebuffers[i].offset;
775 desc.range[i].pfn = pagebuffers[i].pfn;
778 bufferlist[0].iov_base = &desc;
779 bufferlist[0].iov_len = descsize;
780 bufferlist[1].iov_base = buffer;
781 bufferlist[1].iov_len = bufferlen;
782 bufferlist[2].iov_base = &aligned_data;
783 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
785 return hv_ringbuffer_write(channel, bufferlist, 3);
787 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
790 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
791 * using a GPADL Direct packet type.
792 * The buffer includes the vmbus descriptor.
794 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
795 struct vmbus_packet_mpb_array *desc,
797 void *buffer, u32 bufferlen, u64 requestid)
800 u32 packetlen_aligned;
801 struct kvec bufferlist[3];
802 u64 aligned_data = 0;
804 packetlen = desc_size + bufferlen;
805 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
807 /* Setup the descriptor */
808 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
809 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
810 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
811 desc->length8 = (u16)(packetlen_aligned >> 3);
812 desc->transactionid = requestid;
813 desc->rangecount = 1;
815 bufferlist[0].iov_base = desc;
816 bufferlist[0].iov_len = desc_size;
817 bufferlist[1].iov_base = buffer;
818 bufferlist[1].iov_len = bufferlen;
819 bufferlist[2].iov_base = &aligned_data;
820 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
822 return hv_ringbuffer_write(channel, bufferlist, 3);
824 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
827 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
828 * @channel: Pointer to vmbus_channel structure.
829 * @buffer: Pointer to the buffer you want to receive the data into.
830 * @bufferlen: Maximum size of what the the buffer will hold
831 * @buffer_actual_len: The actual size of the data after it was received
832 * @requestid: Identifier of the request
834 * Receives directly from the hyper-v vmbus and puts the data it received
835 * into Buffer. This will receive the data unparsed from hyper-v.
837 * Mainly used by Hyper-V drivers.
840 __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
841 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
844 return hv_ringbuffer_read(channel, buffer, bufferlen,
845 buffer_actual_len, requestid, raw);
849 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
850 u32 bufferlen, u32 *buffer_actual_len,
853 return __vmbus_recvpacket(channel, buffer, bufferlen,
854 buffer_actual_len, requestid, false);
856 EXPORT_SYMBOL(vmbus_recvpacket);
859 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
861 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
862 u32 bufferlen, u32 *buffer_actual_len,
865 return __vmbus_recvpacket(channel, buffer, bufferlen,
866 buffer_actual_len, requestid, true);
868 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);