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;
79 spin_lock_irqsave(&newchannel->lock, flags);
80 if (newchannel->state == CHANNEL_OPEN_STATE) {
81 newchannel->state = CHANNEL_OPENING_STATE;
83 spin_unlock_irqrestore(&newchannel->lock, flags);
86 spin_unlock_irqrestore(&newchannel->lock, flags);
88 newchannel->onchannel_callback = onchannelcallback;
89 newchannel->channel_callback_context = context;
91 /* Allocate the ring buffer */
92 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
93 GFP_KERNEL|__GFP_ZERO,
94 get_order(send_ringbuffer_size +
95 recv_ringbuffer_size));
98 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
99 get_order(send_ringbuffer_size +
100 recv_ringbuffer_size));
102 out = (void *)page_address(page);
109 in = (void *)((unsigned long)out + send_ringbuffer_size);
111 newchannel->ringbuffer_pages = out;
112 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
113 recv_ringbuffer_size) >> PAGE_SHIFT;
115 ret = hv_ringbuffer_init(
116 &newchannel->outbound, out, send_ringbuffer_size);
123 ret = hv_ringbuffer_init(
124 &newchannel->inbound, in, recv_ringbuffer_size);
131 /* Establish the gpadl for the ring buffer */
132 newchannel->ringbuffer_gpadlhandle = 0;
134 ret = vmbus_establish_gpadl(newchannel,
135 newchannel->outbound.ring_buffer,
136 send_ringbuffer_size +
137 recv_ringbuffer_size,
138 &newchannel->ringbuffer_gpadlhandle);
145 /* Create and init the channel open message */
146 open_info = kmalloc(sizeof(*open_info) +
147 sizeof(struct vmbus_channel_open_channel),
154 init_completion(&open_info->waitevent);
156 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
157 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
158 open_msg->openid = newchannel->offermsg.child_relid;
159 open_msg->child_relid = newchannel->offermsg.child_relid;
160 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
161 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
163 open_msg->target_vp = newchannel->target_vp;
165 if (userdatalen > MAX_USER_DEFINED_BYTES) {
171 memcpy(open_msg->userdata, userdata, userdatalen);
173 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
174 list_add_tail(&open_info->msglistentry,
175 &vmbus_connection.chn_msg_list);
176 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
178 ret = vmbus_post_msg(open_msg,
179 sizeof(struct vmbus_channel_open_channel));
186 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
192 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
193 list_del(&open_info->msglistentry);
194 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
196 if (open_info->response.open_result.status) {
201 newchannel->state = CHANNEL_OPENED_STATE;
206 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
207 list_del(&open_info->msglistentry);
208 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
211 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
214 free_pages((unsigned long)out,
215 get_order(send_ringbuffer_size + recv_ringbuffer_size));
217 newchannel->state = CHANNEL_OPEN_STATE;
220 EXPORT_SYMBOL_GPL(vmbus_open);
222 /* Used for Hyper-V Socket: a guest client's connect() to the host */
223 int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
224 const uuid_le *shv_host_servie_id)
226 struct vmbus_channel_tl_connect_request conn_msg;
228 memset(&conn_msg, 0, sizeof(conn_msg));
229 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
230 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
231 conn_msg.host_service_id = *shv_host_servie_id;
233 return vmbus_post_msg(&conn_msg, sizeof(conn_msg));
235 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
238 * create_gpadl_header - Creates a gpadl for the specified buffer
240 static int create_gpadl_header(void *kbuffer, u32 size,
241 struct vmbus_channel_msginfo **msginfo)
245 struct vmbus_channel_gpadl_header *gpadl_header;
246 struct vmbus_channel_gpadl_body *gpadl_body;
247 struct vmbus_channel_msginfo *msgheader;
248 struct vmbus_channel_msginfo *msgbody = NULL;
251 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
253 pagecount = size >> PAGE_SHIFT;
255 /* do we need a gpadl body msg */
256 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
257 sizeof(struct vmbus_channel_gpadl_header) -
258 sizeof(struct gpa_range);
259 pfncount = pfnsize / sizeof(u64);
261 if (pagecount > pfncount) {
262 /* we need a gpadl body */
263 /* fill in the header */
264 msgsize = sizeof(struct vmbus_channel_msginfo) +
265 sizeof(struct vmbus_channel_gpadl_header) +
266 sizeof(struct gpa_range) + pfncount * sizeof(u64);
267 msgheader = kzalloc(msgsize, GFP_KERNEL);
271 INIT_LIST_HEAD(&msgheader->submsglist);
272 msgheader->msgsize = msgsize;
274 gpadl_header = (struct vmbus_channel_gpadl_header *)
276 gpadl_header->rangecount = 1;
277 gpadl_header->range_buflen = sizeof(struct gpa_range) +
278 pagecount * sizeof(u64);
279 gpadl_header->range[0].byte_offset = 0;
280 gpadl_header->range[0].byte_count = size;
281 for (i = 0; i < pfncount; i++)
282 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
283 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
284 *msginfo = msgheader;
287 pfnleft = pagecount - pfncount;
289 /* how many pfns can we fit */
290 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
291 sizeof(struct vmbus_channel_gpadl_body);
292 pfncount = pfnsize / sizeof(u64);
294 /* fill in the body */
296 if (pfnleft > pfncount)
301 msgsize = sizeof(struct vmbus_channel_msginfo) +
302 sizeof(struct vmbus_channel_gpadl_body) +
303 pfncurr * sizeof(u64);
304 msgbody = kzalloc(msgsize, GFP_KERNEL);
307 struct vmbus_channel_msginfo *pos = NULL;
308 struct vmbus_channel_msginfo *tmp = NULL;
310 * Free up all the allocated messages.
312 list_for_each_entry_safe(pos, tmp,
313 &msgheader->submsglist,
316 list_del(&pos->msglistentry);
323 msgbody->msgsize = msgsize;
325 (struct vmbus_channel_gpadl_body *)msgbody->msg;
328 * Gpadl is u32 and we are using a pointer which could
330 * This is governed by the guest/host protocol and
331 * so the hypervisor gurantees that this is ok.
333 for (i = 0; i < pfncurr; i++)
334 gpadl_body->pfn[i] = slow_virt_to_phys(
335 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
338 /* add to msg header */
339 list_add_tail(&msgbody->msglistentry,
340 &msgheader->submsglist);
345 /* everything fits in a header */
346 msgsize = sizeof(struct vmbus_channel_msginfo) +
347 sizeof(struct vmbus_channel_gpadl_header) +
348 sizeof(struct gpa_range) + pagecount * sizeof(u64);
349 msgheader = kzalloc(msgsize, GFP_KERNEL);
350 if (msgheader == NULL)
353 INIT_LIST_HEAD(&msgheader->submsglist);
354 msgheader->msgsize = msgsize;
356 gpadl_header = (struct vmbus_channel_gpadl_header *)
358 gpadl_header->rangecount = 1;
359 gpadl_header->range_buflen = sizeof(struct gpa_range) +
360 pagecount * sizeof(u64);
361 gpadl_header->range[0].byte_offset = 0;
362 gpadl_header->range[0].byte_count = size;
363 for (i = 0; i < pagecount; i++)
364 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
365 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
367 *msginfo = msgheader;
378 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
380 * @channel: a channel
381 * @kbuffer: from kmalloc or vmalloc
382 * @size: page-size multiple
383 * @gpadl_handle: some funky thing
385 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
386 u32 size, u32 *gpadl_handle)
388 struct vmbus_channel_gpadl_header *gpadlmsg;
389 struct vmbus_channel_gpadl_body *gpadl_body;
390 struct vmbus_channel_msginfo *msginfo = NULL;
391 struct vmbus_channel_msginfo *submsginfo, *tmp;
392 struct list_head *curr;
393 u32 next_gpadl_handle;
398 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
400 ret = create_gpadl_header(kbuffer, size, &msginfo);
404 init_completion(&msginfo->waitevent);
406 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
407 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
408 gpadlmsg->child_relid = channel->offermsg.child_relid;
409 gpadlmsg->gpadl = next_gpadl_handle;
412 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
413 list_add_tail(&msginfo->msglistentry,
414 &vmbus_connection.chn_msg_list);
416 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
418 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
423 list_for_each(curr, &msginfo->submsglist) {
424 submsginfo = (struct vmbus_channel_msginfo *)curr;
426 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
428 gpadl_body->header.msgtype =
429 CHANNELMSG_GPADL_BODY;
430 gpadl_body->gpadl = next_gpadl_handle;
432 ret = vmbus_post_msg(gpadl_body,
433 submsginfo->msgsize -
434 sizeof(*submsginfo));
439 wait_for_completion(&msginfo->waitevent);
441 /* At this point, we received the gpadl created msg */
442 *gpadl_handle = gpadlmsg->gpadl;
445 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
446 list_del(&msginfo->msglistentry);
447 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
448 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
456 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
459 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
461 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
463 struct vmbus_channel_gpadl_teardown *msg;
464 struct vmbus_channel_msginfo *info;
468 info = kmalloc(sizeof(*info) +
469 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
473 init_completion(&info->waitevent);
475 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
477 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
478 msg->child_relid = channel->offermsg.child_relid;
479 msg->gpadl = gpadl_handle;
481 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
482 list_add_tail(&info->msglistentry,
483 &vmbus_connection.chn_msg_list);
484 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
485 ret = vmbus_post_msg(msg,
486 sizeof(struct vmbus_channel_gpadl_teardown));
491 wait_for_completion(&info->waitevent);
494 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
495 list_del(&info->msglistentry);
496 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
501 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
503 static void reset_channel_cb(void *arg)
505 struct vmbus_channel *channel = arg;
507 channel->onchannel_callback = NULL;
510 static int vmbus_close_internal(struct vmbus_channel *channel)
512 struct vmbus_channel_close_channel *msg;
513 struct tasklet_struct *tasklet;
517 * process_chn_event(), running in the tasklet, can race
518 * with vmbus_close_internal() in the case of SMP guest, e.g., when
519 * the former is accessing channel->inbound.ring_buffer, the latter
520 * could be freeing the ring_buffer pages.
522 * To resolve the race, we can serialize them by disabling the
523 * tasklet when the latter is running here.
525 tasklet = hv_context.event_dpc[channel->target_cpu];
526 tasklet_disable(tasklet);
529 * In case a device driver's probe() fails (e.g.,
530 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
531 * rescinded later (e.g., we dynamically disble an Integrated Service
532 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
533 * here we should skip most of the below cleanup work.
535 if (channel->state != CHANNEL_OPENED_STATE) {
540 channel->state = CHANNEL_OPEN_STATE;
541 channel->sc_creation_callback = NULL;
542 /* Stop callback and cancel the timer asap */
543 if (channel->target_cpu != get_cpu()) {
545 smp_call_function_single(channel->target_cpu, reset_channel_cb,
548 reset_channel_cb(channel);
552 /* Send a closing message */
554 msg = &channel->close_msg.msg;
556 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
557 msg->child_relid = channel->offermsg.child_relid;
559 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
562 pr_err("Close failed: close post msg return is %d\n", ret);
564 * If we failed to post the close msg,
565 * it is perhaps better to leak memory.
570 /* Tear down the gpadl for the channel's ring buffer */
571 if (channel->ringbuffer_gpadlhandle) {
572 ret = vmbus_teardown_gpadl(channel,
573 channel->ringbuffer_gpadlhandle);
575 pr_err("Close failed: teardown gpadl return %d\n", ret);
577 * If we failed to teardown gpadl,
578 * it is perhaps better to leak memory.
584 /* Cleanup the ring buffers for this channel */
585 hv_ringbuffer_cleanup(&channel->outbound);
586 hv_ringbuffer_cleanup(&channel->inbound);
588 free_pages((unsigned long)channel->ringbuffer_pages,
589 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
592 tasklet_enable(tasklet);
598 * vmbus_close - Close the specified channel
600 void vmbus_close(struct vmbus_channel *channel)
602 struct list_head *cur, *tmp;
603 struct vmbus_channel *cur_channel;
605 if (channel->primary_channel != NULL) {
607 * We will only close sub-channels when
608 * the primary is closed.
613 * Close all the sub-channels first and then close the
616 list_for_each_safe(cur, tmp, &channel->sc_list) {
617 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
618 if (cur_channel->state != CHANNEL_OPENED_STATE)
620 vmbus_close_internal(cur_channel);
623 * Now close the primary.
625 vmbus_close_internal(channel);
627 EXPORT_SYMBOL_GPL(vmbus_close);
629 int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
630 u32 bufferlen, u64 requestid,
631 enum vmbus_packet_type type, u32 flags, bool kick_q)
633 struct vmpacket_descriptor desc;
634 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
635 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
636 struct kvec bufferlist[3];
637 u64 aligned_data = 0;
640 bool lock = channel->acquire_ring_lock;
641 int num_vecs = ((bufferlen != 0) ? 3 : 1);
644 /* Setup the descriptor */
645 desc.type = type; /* VmbusPacketTypeDataInBand; */
646 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
647 /* in 8-bytes granularity */
648 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
649 desc.len8 = (u16)(packetlen_aligned >> 3);
650 desc.trans_id = requestid;
652 bufferlist[0].iov_base = &desc;
653 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
654 bufferlist[1].iov_base = buffer;
655 bufferlist[1].iov_len = bufferlen;
656 bufferlist[2].iov_base = &aligned_data;
657 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
659 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, num_vecs,
663 * Signalling the host is conditional on many factors:
664 * 1. The ring state changed from being empty to non-empty.
665 * This is tracked by the variable "signal".
666 * 2. The variable kick_q tracks if more data will be placed
667 * on the ring. We will not signal if more data is
670 * Based on the channel signal state, we will decide
671 * which signaling policy will be applied.
673 * If we cannot write to the ring-buffer; signal the host
674 * even if we may not have written anything. This is a rare
675 * enough condition that it should not matter.
676 * NOTE: in this case, the hvsock channel is an exception, because
677 * it looks the host side's hvsock implementation has a throttling
678 * mechanism which can hurt the performance otherwise.
681 if (channel->signal_policy)
686 if (((ret == 0) && kick_q && signal) ||
687 (ret && !is_hvsock_channel(channel)))
688 vmbus_setevent(channel);
692 EXPORT_SYMBOL(vmbus_sendpacket_ctl);
695 * vmbus_sendpacket() - Send the specified buffer on the given channel
696 * @channel: Pointer to vmbus_channel structure.
697 * @buffer: Pointer to the buffer you want to receive the data into.
698 * @bufferlen: Maximum size of what the the buffer will hold
699 * @requestid: Identifier of the request
700 * @type: Type of packet that is being send e.g. negotiate, time
703 * Sends data in @buffer directly to hyper-v via the vmbus
704 * This will send the data unparsed to hyper-v.
706 * Mainly used by Hyper-V drivers.
708 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
709 u32 bufferlen, u64 requestid,
710 enum vmbus_packet_type type, u32 flags)
712 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
715 EXPORT_SYMBOL(vmbus_sendpacket);
718 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
719 * packets using a GPADL Direct packet type. This interface allows you
720 * to control notifying the host. This will be useful for sending
721 * batched data. Also the sender can control the send flags
724 int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
725 struct hv_page_buffer pagebuffers[],
726 u32 pagecount, void *buffer, u32 bufferlen,
733 struct vmbus_channel_packet_page_buffer desc;
736 u32 packetlen_aligned;
737 struct kvec bufferlist[3];
738 u64 aligned_data = 0;
740 bool lock = channel->acquire_ring_lock;
742 if (pagecount > MAX_PAGE_BUFFER_COUNT)
747 * Adjust the size down since vmbus_channel_packet_page_buffer is the
748 * largest size we support
750 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
751 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
752 sizeof(struct hv_page_buffer));
753 packetlen = descsize + bufferlen;
754 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
756 /* Setup the descriptor */
757 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
759 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
760 desc.length8 = (u16)(packetlen_aligned >> 3);
761 desc.transactionid = requestid;
762 desc.rangecount = pagecount;
764 for (i = 0; i < pagecount; i++) {
765 desc.range[i].len = pagebuffers[i].len;
766 desc.range[i].offset = pagebuffers[i].offset;
767 desc.range[i].pfn = pagebuffers[i].pfn;
770 bufferlist[0].iov_base = &desc;
771 bufferlist[0].iov_len = descsize;
772 bufferlist[1].iov_base = buffer;
773 bufferlist[1].iov_len = bufferlen;
774 bufferlist[2].iov_base = &aligned_data;
775 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
777 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
781 * Signalling the host is conditional on many factors:
782 * 1. The ring state changed from being empty to non-empty.
783 * This is tracked by the variable "signal".
784 * 2. The variable kick_q tracks if more data will be placed
785 * on the ring. We will not signal if more data is
788 * Based on the channel signal state, we will decide
789 * which signaling policy will be applied.
791 * If we cannot write to the ring-buffer; signal the host
792 * even if we may not have written anything. This is a rare
793 * enough condition that it should not matter.
796 if (channel->signal_policy)
801 if (((ret == 0) && kick_q && signal) || (ret))
802 vmbus_setevent(channel);
806 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
809 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
810 * packets using a GPADL Direct packet type.
812 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
813 struct hv_page_buffer pagebuffers[],
814 u32 pagecount, void *buffer, u32 bufferlen,
817 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
818 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
819 buffer, bufferlen, requestid,
823 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
826 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
827 * using a GPADL Direct packet type.
828 * The buffer includes the vmbus descriptor.
830 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
831 struct vmbus_packet_mpb_array *desc,
833 void *buffer, u32 bufferlen, u64 requestid)
837 u32 packetlen_aligned;
838 struct kvec bufferlist[3];
839 u64 aligned_data = 0;
841 bool lock = channel->acquire_ring_lock;
843 packetlen = desc_size + bufferlen;
844 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
846 /* Setup the descriptor */
847 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
848 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
849 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
850 desc->length8 = (u16)(packetlen_aligned >> 3);
851 desc->transactionid = requestid;
852 desc->rangecount = 1;
854 bufferlist[0].iov_base = desc;
855 bufferlist[0].iov_len = desc_size;
856 bufferlist[1].iov_base = buffer;
857 bufferlist[1].iov_len = bufferlen;
858 bufferlist[2].iov_base = &aligned_data;
859 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
861 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
864 if (ret == 0 && signal)
865 vmbus_setevent(channel);
869 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
872 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
873 * using a GPADL Direct packet type.
875 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
876 struct hv_multipage_buffer *multi_pagebuffer,
877 void *buffer, u32 bufferlen, u64 requestid)
880 struct vmbus_channel_packet_multipage_buffer desc;
883 u32 packetlen_aligned;
884 struct kvec bufferlist[3];
885 u64 aligned_data = 0;
887 bool lock = channel->acquire_ring_lock;
888 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
889 multi_pagebuffer->len);
891 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
895 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
896 * the largest size we support
898 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
899 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
901 packetlen = descsize + bufferlen;
902 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
905 /* Setup the descriptor */
906 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
907 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
908 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
909 desc.length8 = (u16)(packetlen_aligned >> 3);
910 desc.transactionid = requestid;
913 desc.range.len = multi_pagebuffer->len;
914 desc.range.offset = multi_pagebuffer->offset;
916 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
917 pfncount * sizeof(u64));
919 bufferlist[0].iov_base = &desc;
920 bufferlist[0].iov_len = descsize;
921 bufferlist[1].iov_base = buffer;
922 bufferlist[1].iov_len = bufferlen;
923 bufferlist[2].iov_base = &aligned_data;
924 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
926 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
929 if (ret == 0 && signal)
930 vmbus_setevent(channel);
934 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
937 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
938 * @channel: Pointer to vmbus_channel structure.
939 * @buffer: Pointer to the buffer you want to receive the data into.
940 * @bufferlen: Maximum size of what the the buffer will hold
941 * @buffer_actual_len: The actual size of the data after it was received
942 * @requestid: Identifier of the request
944 * Receives directly from the hyper-v vmbus and puts the data it received
945 * into Buffer. This will receive the data unparsed from hyper-v.
947 * Mainly used by Hyper-V drivers.
950 __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
951 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
957 ret = hv_ringbuffer_read(&channel->inbound, buffer, bufferlen,
958 buffer_actual_len, requestid, &signal, raw);
961 vmbus_setevent(channel);
966 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
967 u32 bufferlen, u32 *buffer_actual_len,
970 return __vmbus_recvpacket(channel, buffer, bufferlen,
971 buffer_actual_len, requestid, false);
973 EXPORT_SYMBOL(vmbus_recvpacket);
976 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
978 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
979 u32 bufferlen, u32 *buffer_actual_len,
982 return __vmbus_recvpacket(channel, buffer, bufferlen,
983 buffer_actual_len, requestid, true);
985 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);