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.
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>
34 #include "hyperv_vmbus.h"
36 #define NUM_PAGES_SPANNED(addr, len) \
37 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
39 static unsigned long virt_to_hvpfn(void *addr)
43 if (is_vmalloc_addr(addr))
44 paddr = page_to_phys(vmalloc_to_page(addr)) +
49 return paddr >> PAGE_SHIFT;
53 * vmbus_setevent- Trigger an event notification on the specified
56 void vmbus_setevent(struct vmbus_channel *channel)
58 struct hv_monitor_page *monitorpage;
60 trace_vmbus_setevent(channel);
63 * For channels marked as in "low latency" mode
64 * bypass the monitor page mechanism.
66 if (channel->offermsg.monitor_allocated && !channel->low_latency) {
67 vmbus_send_interrupt(channel->offermsg.child_relid);
69 /* Get the child to parent monitor page */
70 monitorpage = vmbus_connection.monitor_pages[1];
72 sync_set_bit(channel->monitor_bit,
73 (unsigned long *)&monitorpage->trigger_group
74 [channel->monitor_grp].pending);
77 vmbus_set_event(channel);
80 EXPORT_SYMBOL_GPL(vmbus_setevent);
82 /* vmbus_free_ring - drop mapping of ring buffer */
83 void vmbus_free_ring(struct vmbus_channel *channel)
85 hv_ringbuffer_cleanup(&channel->outbound);
86 hv_ringbuffer_cleanup(&channel->inbound);
88 if (channel->ringbuffer_page) {
89 __free_pages(channel->ringbuffer_page,
90 get_order(channel->ringbuffer_pagecount
92 channel->ringbuffer_page = NULL;
95 EXPORT_SYMBOL_GPL(vmbus_free_ring);
97 /* vmbus_alloc_ring - allocate and map pages for ring buffer */
98 int vmbus_alloc_ring(struct vmbus_channel *newchannel,
99 u32 send_size, u32 recv_size)
104 if (send_size % PAGE_SIZE || recv_size % PAGE_SIZE)
107 /* Allocate the ring buffer */
108 order = get_order(send_size + recv_size);
109 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
110 GFP_KERNEL|__GFP_ZERO, order);
113 page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order);
118 newchannel->ringbuffer_page = page;
119 newchannel->ringbuffer_pagecount = (send_size + recv_size) >> PAGE_SHIFT;
120 newchannel->ringbuffer_send_offset = send_size >> PAGE_SHIFT;
124 EXPORT_SYMBOL_GPL(vmbus_alloc_ring);
126 static int __vmbus_open(struct vmbus_channel *newchannel,
127 void *userdata, u32 userdatalen,
128 void (*onchannelcallback)(void *context), void *context)
130 struct vmbus_channel_open_channel *open_msg;
131 struct vmbus_channel_msginfo *open_info = NULL;
132 struct page *page = newchannel->ringbuffer_page;
133 u32 send_pages, recv_pages;
137 if (userdatalen > MAX_USER_DEFINED_BYTES)
140 send_pages = newchannel->ringbuffer_send_offset;
141 recv_pages = newchannel->ringbuffer_pagecount - send_pages;
143 spin_lock_irqsave(&newchannel->lock, flags);
144 if (newchannel->state != CHANNEL_OPEN_STATE) {
145 spin_unlock_irqrestore(&newchannel->lock, flags);
148 spin_unlock_irqrestore(&newchannel->lock, flags);
150 newchannel->state = CHANNEL_OPENING_STATE;
151 newchannel->onchannel_callback = onchannelcallback;
152 newchannel->channel_callback_context = context;
154 err = hv_ringbuffer_init(&newchannel->outbound, page, send_pages);
156 goto error_clean_ring;
158 err = hv_ringbuffer_init(&newchannel->inbound,
159 &page[send_pages], recv_pages);
161 goto error_clean_ring;
163 /* Establish the gpadl for the ring buffer */
164 newchannel->ringbuffer_gpadlhandle = 0;
166 err = vmbus_establish_gpadl(newchannel,
167 page_address(newchannel->ringbuffer_page),
168 (send_pages + recv_pages) << PAGE_SHIFT,
169 &newchannel->ringbuffer_gpadlhandle);
171 goto error_clean_ring;
173 /* Create and init the channel open message */
174 open_info = kmalloc(sizeof(*open_info) +
175 sizeof(struct vmbus_channel_open_channel),
179 goto error_free_gpadl;
182 init_completion(&open_info->waitevent);
183 open_info->waiting_channel = newchannel;
185 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
186 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
187 open_msg->openid = newchannel->offermsg.child_relid;
188 open_msg->child_relid = newchannel->offermsg.child_relid;
189 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
190 open_msg->downstream_ringbuffer_pageoffset = newchannel->ringbuffer_send_offset;
191 open_msg->target_vp = newchannel->target_vp;
194 memcpy(open_msg->userdata, userdata, userdatalen);
196 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
197 list_add_tail(&open_info->msglistentry,
198 &vmbus_connection.chn_msg_list);
199 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
201 if (newchannel->rescind) {
203 goto error_free_info;
206 err = vmbus_post_msg(open_msg,
207 sizeof(struct vmbus_channel_open_channel), true);
209 trace_vmbus_open(open_msg, err);
212 goto error_clean_msglist;
214 wait_for_completion(&open_info->waitevent);
216 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
217 list_del(&open_info->msglistentry);
218 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
220 if (newchannel->rescind) {
222 goto error_free_info;
225 if (open_info->response.open_result.status) {
227 goto error_free_info;
230 newchannel->state = CHANNEL_OPENED_STATE;
235 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
236 list_del(&open_info->msglistentry);
237 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
241 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
242 newchannel->ringbuffer_gpadlhandle = 0;
244 hv_ringbuffer_cleanup(&newchannel->outbound);
245 hv_ringbuffer_cleanup(&newchannel->inbound);
246 newchannel->state = CHANNEL_OPEN_STATE;
251 * vmbus_connect_ring - Open the channel but reuse ring buffer
253 int vmbus_connect_ring(struct vmbus_channel *newchannel,
254 void (*onchannelcallback)(void *context), void *context)
256 return __vmbus_open(newchannel, NULL, 0, onchannelcallback, context);
258 EXPORT_SYMBOL_GPL(vmbus_connect_ring);
261 * vmbus_open - Open the specified channel.
263 int vmbus_open(struct vmbus_channel *newchannel,
264 u32 send_ringbuffer_size, u32 recv_ringbuffer_size,
265 void *userdata, u32 userdatalen,
266 void (*onchannelcallback)(void *context), void *context)
270 err = vmbus_alloc_ring(newchannel, send_ringbuffer_size,
271 recv_ringbuffer_size);
275 err = __vmbus_open(newchannel, userdata, userdatalen,
276 onchannelcallback, context);
278 vmbus_free_ring(newchannel);
282 EXPORT_SYMBOL_GPL(vmbus_open);
284 /* Used for Hyper-V Socket: a guest client's connect() to the host */
285 int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
286 const uuid_le *shv_host_servie_id)
288 struct vmbus_channel_tl_connect_request conn_msg;
291 memset(&conn_msg, 0, sizeof(conn_msg));
292 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
293 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
294 conn_msg.host_service_id = *shv_host_servie_id;
296 ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
298 trace_vmbus_send_tl_connect_request(&conn_msg, ret);
302 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
305 * create_gpadl_header - Creates a gpadl for the specified buffer
307 static int create_gpadl_header(void *kbuffer, u32 size,
308 struct vmbus_channel_msginfo **msginfo)
312 struct vmbus_channel_gpadl_header *gpadl_header;
313 struct vmbus_channel_gpadl_body *gpadl_body;
314 struct vmbus_channel_msginfo *msgheader;
315 struct vmbus_channel_msginfo *msgbody = NULL;
318 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
320 pagecount = size >> PAGE_SHIFT;
322 /* do we need a gpadl body msg */
323 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
324 sizeof(struct vmbus_channel_gpadl_header) -
325 sizeof(struct gpa_range);
326 pfncount = pfnsize / sizeof(u64);
328 if (pagecount > pfncount) {
329 /* we need a gpadl body */
330 /* fill in the header */
331 msgsize = sizeof(struct vmbus_channel_msginfo) +
332 sizeof(struct vmbus_channel_gpadl_header) +
333 sizeof(struct gpa_range) + pfncount * sizeof(u64);
334 msgheader = kzalloc(msgsize, GFP_KERNEL);
338 INIT_LIST_HEAD(&msgheader->submsglist);
339 msgheader->msgsize = msgsize;
341 gpadl_header = (struct vmbus_channel_gpadl_header *)
343 gpadl_header->rangecount = 1;
344 gpadl_header->range_buflen = sizeof(struct gpa_range) +
345 pagecount * sizeof(u64);
346 gpadl_header->range[0].byte_offset = 0;
347 gpadl_header->range[0].byte_count = size;
348 for (i = 0; i < pfncount; i++)
349 gpadl_header->range[0].pfn_array[i] = virt_to_hvpfn(
350 kbuffer + PAGE_SIZE * i);
351 *msginfo = msgheader;
354 pfnleft = pagecount - pfncount;
356 /* how many pfns can we fit */
357 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
358 sizeof(struct vmbus_channel_gpadl_body);
359 pfncount = pfnsize / sizeof(u64);
361 /* fill in the body */
363 if (pfnleft > pfncount)
368 msgsize = sizeof(struct vmbus_channel_msginfo) +
369 sizeof(struct vmbus_channel_gpadl_body) +
370 pfncurr * sizeof(u64);
371 msgbody = kzalloc(msgsize, GFP_KERNEL);
374 struct vmbus_channel_msginfo *pos = NULL;
375 struct vmbus_channel_msginfo *tmp = NULL;
377 * Free up all the allocated messages.
379 list_for_each_entry_safe(pos, tmp,
380 &msgheader->submsglist,
383 list_del(&pos->msglistentry);
390 msgbody->msgsize = msgsize;
392 (struct vmbus_channel_gpadl_body *)msgbody->msg;
395 * Gpadl is u32 and we are using a pointer which could
397 * This is governed by the guest/host protocol and
398 * so the hypervisor guarantees that this is ok.
400 for (i = 0; i < pfncurr; i++)
401 gpadl_body->pfn[i] = virt_to_hvpfn(
402 kbuffer + PAGE_SIZE * (pfnsum + i));
404 /* add to msg header */
405 list_add_tail(&msgbody->msglistentry,
406 &msgheader->submsglist);
411 /* everything fits in a header */
412 msgsize = sizeof(struct vmbus_channel_msginfo) +
413 sizeof(struct vmbus_channel_gpadl_header) +
414 sizeof(struct gpa_range) + pagecount * sizeof(u64);
415 msgheader = kzalloc(msgsize, GFP_KERNEL);
416 if (msgheader == NULL)
419 INIT_LIST_HEAD(&msgheader->submsglist);
420 msgheader->msgsize = msgsize;
422 gpadl_header = (struct vmbus_channel_gpadl_header *)
424 gpadl_header->rangecount = 1;
425 gpadl_header->range_buflen = sizeof(struct gpa_range) +
426 pagecount * sizeof(u64);
427 gpadl_header->range[0].byte_offset = 0;
428 gpadl_header->range[0].byte_count = size;
429 for (i = 0; i < pagecount; i++)
430 gpadl_header->range[0].pfn_array[i] = virt_to_hvpfn(
431 kbuffer + PAGE_SIZE * i);
433 *msginfo = msgheader;
444 * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
446 * @channel: a channel
447 * @kbuffer: from kmalloc or vmalloc
448 * @size: page-size multiple
449 * @gpadl_handle: some funky thing
451 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
452 u32 size, u32 *gpadl_handle)
454 struct vmbus_channel_gpadl_header *gpadlmsg;
455 struct vmbus_channel_gpadl_body *gpadl_body;
456 struct vmbus_channel_msginfo *msginfo = NULL;
457 struct vmbus_channel_msginfo *submsginfo, *tmp;
458 struct list_head *curr;
459 u32 next_gpadl_handle;
464 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
466 ret = create_gpadl_header(kbuffer, size, &msginfo);
470 init_completion(&msginfo->waitevent);
471 msginfo->waiting_channel = channel;
473 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
474 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
475 gpadlmsg->child_relid = channel->offermsg.child_relid;
476 gpadlmsg->gpadl = next_gpadl_handle;
479 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
480 list_add_tail(&msginfo->msglistentry,
481 &vmbus_connection.chn_msg_list);
483 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
485 if (channel->rescind) {
490 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
491 sizeof(*msginfo), true);
493 trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
498 list_for_each(curr, &msginfo->submsglist) {
499 submsginfo = (struct vmbus_channel_msginfo *)curr;
501 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
503 gpadl_body->header.msgtype =
504 CHANNELMSG_GPADL_BODY;
505 gpadl_body->gpadl = next_gpadl_handle;
507 ret = vmbus_post_msg(gpadl_body,
508 submsginfo->msgsize - sizeof(*submsginfo),
511 trace_vmbus_establish_gpadl_body(gpadl_body, ret);
517 wait_for_completion(&msginfo->waitevent);
519 if (msginfo->response.gpadl_created.creation_status != 0) {
520 pr_err("Failed to establish GPADL: err = 0x%x\n",
521 msginfo->response.gpadl_created.creation_status);
527 if (channel->rescind) {
532 /* At this point, we received the gpadl created msg */
533 *gpadl_handle = gpadlmsg->gpadl;
536 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
537 list_del(&msginfo->msglistentry);
538 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
539 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
547 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
550 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
552 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
554 struct vmbus_channel_gpadl_teardown *msg;
555 struct vmbus_channel_msginfo *info;
559 info = kmalloc(sizeof(*info) +
560 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
564 init_completion(&info->waitevent);
565 info->waiting_channel = channel;
567 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
569 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
570 msg->child_relid = channel->offermsg.child_relid;
571 msg->gpadl = gpadl_handle;
573 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
574 list_add_tail(&info->msglistentry,
575 &vmbus_connection.chn_msg_list);
576 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
578 if (channel->rescind)
581 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
584 trace_vmbus_teardown_gpadl(msg, ret);
589 wait_for_completion(&info->waitevent);
593 * If the channel has been rescinded;
594 * we will be awakened by the rescind
595 * handler; set the error code to zero so we don't leak memory.
597 if (channel->rescind)
600 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
601 list_del(&info->msglistentry);
602 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
607 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
609 static void reset_channel_cb(void *arg)
611 struct vmbus_channel *channel = arg;
613 channel->onchannel_callback = NULL;
616 void vmbus_reset_channel_cb(struct vmbus_channel *channel)
619 * vmbus_on_event(), running in the per-channel tasklet, can race
620 * with vmbus_close_internal() in the case of SMP guest, e.g., when
621 * the former is accessing channel->inbound.ring_buffer, the latter
622 * could be freeing the ring_buffer pages, so here we must stop it
625 tasklet_disable(&channel->callback_event);
627 channel->sc_creation_callback = NULL;
629 /* Stop the callback asap */
630 if (channel->target_cpu != get_cpu()) {
632 smp_call_function_single(channel->target_cpu, reset_channel_cb,
635 reset_channel_cb(channel);
639 /* Re-enable tasklet for use on re-open */
640 tasklet_enable(&channel->callback_event);
643 static int vmbus_close_internal(struct vmbus_channel *channel)
645 struct vmbus_channel_close_channel *msg;
648 vmbus_reset_channel_cb(channel);
651 * In case a device driver's probe() fails (e.g.,
652 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
653 * rescinded later (e.g., we dynamically disable an Integrated Service
654 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
655 * here we should skip most of the below cleanup work.
657 if (channel->state != CHANNEL_OPENED_STATE)
660 channel->state = CHANNEL_OPEN_STATE;
662 /* Send a closing message */
664 msg = &channel->close_msg.msg;
666 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
667 msg->child_relid = channel->offermsg.child_relid;
669 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
672 trace_vmbus_close_internal(msg, ret);
675 pr_err("Close failed: close post msg return is %d\n", ret);
677 * If we failed to post the close msg,
678 * it is perhaps better to leak memory.
682 /* Tear down the gpadl for the channel's ring buffer */
683 else if (channel->ringbuffer_gpadlhandle) {
684 ret = vmbus_teardown_gpadl(channel,
685 channel->ringbuffer_gpadlhandle);
687 pr_err("Close failed: teardown gpadl return %d\n", ret);
689 * If we failed to teardown gpadl,
690 * it is perhaps better to leak memory.
694 channel->ringbuffer_gpadlhandle = 0;
700 /* disconnect ring - close all channels */
701 int vmbus_disconnect_ring(struct vmbus_channel *channel)
703 struct vmbus_channel *cur_channel, *tmp;
708 if (channel->primary_channel != NULL)
711 /* Snapshot the list of subchannels */
712 spin_lock_irqsave(&channel->lock, flags);
713 list_splice_init(&channel->sc_list, &list);
715 spin_unlock_irqrestore(&channel->lock, flags);
717 list_for_each_entry_safe(cur_channel, tmp, &list, sc_list) {
718 if (cur_channel->rescind)
719 wait_for_completion(&cur_channel->rescind_event);
721 mutex_lock(&vmbus_connection.channel_mutex);
722 if (vmbus_close_internal(cur_channel) == 0) {
723 vmbus_free_ring(cur_channel);
725 if (cur_channel->rescind)
726 hv_process_channel_removal(cur_channel);
728 mutex_unlock(&vmbus_connection.channel_mutex);
732 * Now close the primary.
734 mutex_lock(&vmbus_connection.channel_mutex);
735 ret = vmbus_close_internal(channel);
736 mutex_unlock(&vmbus_connection.channel_mutex);
740 EXPORT_SYMBOL_GPL(vmbus_disconnect_ring);
743 * vmbus_close - Close the specified channel
745 void vmbus_close(struct vmbus_channel *channel)
747 if (vmbus_disconnect_ring(channel) == 0)
748 vmbus_free_ring(channel);
750 EXPORT_SYMBOL_GPL(vmbus_close);
753 * vmbus_sendpacket() - Send the specified buffer on the given channel
754 * @channel: Pointer to vmbus_channel structure
755 * @buffer: Pointer to the buffer you want to send the data from.
756 * @bufferlen: Maximum size of what the buffer holds.
757 * @requestid: Identifier of the request
758 * @type: Type of packet that is being sent e.g. negotiate, time
760 * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
762 * Sends data in @buffer directly to Hyper-V via the vmbus.
763 * This will send the data unparsed to Hyper-V.
765 * Mainly used by Hyper-V drivers.
767 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
768 u32 bufferlen, u64 requestid,
769 enum vmbus_packet_type type, u32 flags)
771 struct vmpacket_descriptor desc;
772 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
773 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
774 struct kvec bufferlist[3];
775 u64 aligned_data = 0;
776 int num_vecs = ((bufferlen != 0) ? 3 : 1);
779 /* Setup the descriptor */
780 desc.type = type; /* VmbusPacketTypeDataInBand; */
781 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
782 /* in 8-bytes granularity */
783 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
784 desc.len8 = (u16)(packetlen_aligned >> 3);
785 desc.trans_id = requestid;
787 bufferlist[0].iov_base = &desc;
788 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
789 bufferlist[1].iov_base = buffer;
790 bufferlist[1].iov_len = bufferlen;
791 bufferlist[2].iov_base = &aligned_data;
792 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
794 return hv_ringbuffer_write(channel, bufferlist, num_vecs);
796 EXPORT_SYMBOL(vmbus_sendpacket);
799 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
800 * packets using a GPADL Direct packet type. This interface allows you
801 * to control notifying the host. This will be useful for sending
802 * batched data. Also the sender can control the send flags
805 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
806 struct hv_page_buffer pagebuffers[],
807 u32 pagecount, void *buffer, u32 bufferlen,
811 struct vmbus_channel_packet_page_buffer desc;
814 u32 packetlen_aligned;
815 struct kvec bufferlist[3];
816 u64 aligned_data = 0;
818 if (pagecount > MAX_PAGE_BUFFER_COUNT)
822 * Adjust the size down since vmbus_channel_packet_page_buffer is the
823 * largest size we support
825 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
826 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
827 sizeof(struct hv_page_buffer));
828 packetlen = descsize + bufferlen;
829 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
831 /* Setup the descriptor */
832 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
833 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
834 desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
835 desc.length8 = (u16)(packetlen_aligned >> 3);
836 desc.transactionid = requestid;
838 desc.rangecount = pagecount;
840 for (i = 0; i < pagecount; i++) {
841 desc.range[i].len = pagebuffers[i].len;
842 desc.range[i].offset = pagebuffers[i].offset;
843 desc.range[i].pfn = pagebuffers[i].pfn;
846 bufferlist[0].iov_base = &desc;
847 bufferlist[0].iov_len = descsize;
848 bufferlist[1].iov_base = buffer;
849 bufferlist[1].iov_len = bufferlen;
850 bufferlist[2].iov_base = &aligned_data;
851 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
853 return hv_ringbuffer_write(channel, bufferlist, 3);
855 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
858 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
859 * using a GPADL Direct packet type.
860 * The buffer includes the vmbus descriptor.
862 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
863 struct vmbus_packet_mpb_array *desc,
865 void *buffer, u32 bufferlen, u64 requestid)
868 u32 packetlen_aligned;
869 struct kvec bufferlist[3];
870 u64 aligned_data = 0;
872 packetlen = desc_size + bufferlen;
873 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
875 /* Setup the descriptor */
876 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
877 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
878 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
879 desc->length8 = (u16)(packetlen_aligned >> 3);
880 desc->transactionid = requestid;
882 desc->rangecount = 1;
884 bufferlist[0].iov_base = desc;
885 bufferlist[0].iov_len = desc_size;
886 bufferlist[1].iov_base = buffer;
887 bufferlist[1].iov_len = bufferlen;
888 bufferlist[2].iov_base = &aligned_data;
889 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
891 return hv_ringbuffer_write(channel, bufferlist, 3);
893 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
896 * __vmbus_recvpacket() - Retrieve the user packet on the specified channel
897 * @channel: Pointer to vmbus_channel structure
898 * @buffer: Pointer to the buffer you want to receive the data into.
899 * @bufferlen: Maximum size of what the buffer can hold.
900 * @buffer_actual_len: The actual size of the data after it was received.
901 * @requestid: Identifier of the request
902 * @raw: true means keep the vmpacket_descriptor header in the received data.
904 * Receives directly from the hyper-v vmbus and puts the data it received
905 * into Buffer. This will receive the data unparsed from hyper-v.
907 * Mainly used by Hyper-V drivers.
910 __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
911 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
914 return hv_ringbuffer_read(channel, buffer, bufferlen,
915 buffer_actual_len, requestid, raw);
919 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
920 u32 bufferlen, u32 *buffer_actual_len,
923 return __vmbus_recvpacket(channel, buffer, bufferlen,
924 buffer_actual_len, requestid, false);
926 EXPORT_SYMBOL(vmbus_recvpacket);
929 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
931 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
932 u32 bufferlen, u32 *buffer_actual_len,
935 return __vmbus_recvpacket(channel, buffer, bufferlen,
936 buffer_actual_len, requestid, true);
938 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);