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);
217 /* Used for Hyper-V Socket: a guest client's connect() to the host */
218 int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
219 const uuid_le *shv_host_servie_id)
221 struct vmbus_channel_tl_connect_request conn_msg;
223 memset(&conn_msg, 0, sizeof(conn_msg));
224 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
225 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
226 conn_msg.host_service_id = *shv_host_servie_id;
228 return vmbus_post_msg(&conn_msg, sizeof(conn_msg));
230 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
233 * create_gpadl_header - Creates a gpadl for the specified buffer
235 static int create_gpadl_header(void *kbuffer, u32 size,
236 struct vmbus_channel_msginfo **msginfo)
240 struct vmbus_channel_gpadl_header *gpadl_header;
241 struct vmbus_channel_gpadl_body *gpadl_body;
242 struct vmbus_channel_msginfo *msgheader;
243 struct vmbus_channel_msginfo *msgbody = NULL;
246 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
248 pagecount = size >> PAGE_SHIFT;
250 /* do we need a gpadl body msg */
251 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
252 sizeof(struct vmbus_channel_gpadl_header) -
253 sizeof(struct gpa_range);
254 pfncount = pfnsize / sizeof(u64);
256 if (pagecount > pfncount) {
257 /* we need a gpadl body */
258 /* fill in the header */
259 msgsize = sizeof(struct vmbus_channel_msginfo) +
260 sizeof(struct vmbus_channel_gpadl_header) +
261 sizeof(struct gpa_range) + pfncount * sizeof(u64);
262 msgheader = kzalloc(msgsize, GFP_KERNEL);
266 INIT_LIST_HEAD(&msgheader->submsglist);
267 msgheader->msgsize = msgsize;
269 gpadl_header = (struct vmbus_channel_gpadl_header *)
271 gpadl_header->rangecount = 1;
272 gpadl_header->range_buflen = sizeof(struct gpa_range) +
273 pagecount * sizeof(u64);
274 gpadl_header->range[0].byte_offset = 0;
275 gpadl_header->range[0].byte_count = size;
276 for (i = 0; i < pfncount; i++)
277 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
278 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
279 *msginfo = msgheader;
282 pfnleft = pagecount - pfncount;
284 /* how many pfns can we fit */
285 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
286 sizeof(struct vmbus_channel_gpadl_body);
287 pfncount = pfnsize / sizeof(u64);
289 /* fill in the body */
291 if (pfnleft > pfncount)
296 msgsize = sizeof(struct vmbus_channel_msginfo) +
297 sizeof(struct vmbus_channel_gpadl_body) +
298 pfncurr * sizeof(u64);
299 msgbody = kzalloc(msgsize, GFP_KERNEL);
302 struct vmbus_channel_msginfo *pos = NULL;
303 struct vmbus_channel_msginfo *tmp = NULL;
305 * Free up all the allocated messages.
307 list_for_each_entry_safe(pos, tmp,
308 &msgheader->submsglist,
311 list_del(&pos->msglistentry);
318 msgbody->msgsize = msgsize;
320 (struct vmbus_channel_gpadl_body *)msgbody->msg;
323 * Gpadl is u32 and we are using a pointer which could
325 * This is governed by the guest/host protocol and
326 * so the hypervisor gurantees that this is ok.
328 for (i = 0; i < pfncurr; i++)
329 gpadl_body->pfn[i] = slow_virt_to_phys(
330 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
333 /* add to msg header */
334 list_add_tail(&msgbody->msglistentry,
335 &msgheader->submsglist);
340 /* everything fits in a header */
341 msgsize = sizeof(struct vmbus_channel_msginfo) +
342 sizeof(struct vmbus_channel_gpadl_header) +
343 sizeof(struct gpa_range) + pagecount * sizeof(u64);
344 msgheader = kzalloc(msgsize, GFP_KERNEL);
345 if (msgheader == NULL)
348 INIT_LIST_HEAD(&msgheader->submsglist);
349 msgheader->msgsize = msgsize;
351 gpadl_header = (struct vmbus_channel_gpadl_header *)
353 gpadl_header->rangecount = 1;
354 gpadl_header->range_buflen = sizeof(struct gpa_range) +
355 pagecount * sizeof(u64);
356 gpadl_header->range[0].byte_offset = 0;
357 gpadl_header->range[0].byte_count = size;
358 for (i = 0; i < pagecount; i++)
359 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
360 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
362 *msginfo = msgheader;
373 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
375 * @channel: a channel
376 * @kbuffer: from kmalloc or vmalloc
377 * @size: page-size multiple
378 * @gpadl_handle: some funky thing
380 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
381 u32 size, u32 *gpadl_handle)
383 struct vmbus_channel_gpadl_header *gpadlmsg;
384 struct vmbus_channel_gpadl_body *gpadl_body;
385 struct vmbus_channel_msginfo *msginfo = NULL;
386 struct vmbus_channel_msginfo *submsginfo, *tmp;
387 struct list_head *curr;
388 u32 next_gpadl_handle;
393 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
395 ret = create_gpadl_header(kbuffer, size, &msginfo);
399 init_completion(&msginfo->waitevent);
401 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
402 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
403 gpadlmsg->child_relid = channel->offermsg.child_relid;
404 gpadlmsg->gpadl = next_gpadl_handle;
407 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
408 list_add_tail(&msginfo->msglistentry,
409 &vmbus_connection.chn_msg_list);
411 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
413 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
418 list_for_each(curr, &msginfo->submsglist) {
419 submsginfo = (struct vmbus_channel_msginfo *)curr;
421 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
423 gpadl_body->header.msgtype =
424 CHANNELMSG_GPADL_BODY;
425 gpadl_body->gpadl = next_gpadl_handle;
427 ret = vmbus_post_msg(gpadl_body,
428 submsginfo->msgsize -
429 sizeof(*submsginfo));
434 wait_for_completion(&msginfo->waitevent);
436 /* At this point, we received the gpadl created msg */
437 *gpadl_handle = gpadlmsg->gpadl;
440 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
441 list_del(&msginfo->msglistentry);
442 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
443 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
451 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
454 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
456 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
458 struct vmbus_channel_gpadl_teardown *msg;
459 struct vmbus_channel_msginfo *info;
463 info = kmalloc(sizeof(*info) +
464 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
468 init_completion(&info->waitevent);
470 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
472 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
473 msg->child_relid = channel->offermsg.child_relid;
474 msg->gpadl = gpadl_handle;
476 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
477 list_add_tail(&info->msglistentry,
478 &vmbus_connection.chn_msg_list);
479 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
480 ret = vmbus_post_msg(msg,
481 sizeof(struct vmbus_channel_gpadl_teardown));
486 wait_for_completion(&info->waitevent);
489 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
490 list_del(&info->msglistentry);
491 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
496 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
498 static void reset_channel_cb(void *arg)
500 struct vmbus_channel *channel = arg;
502 channel->onchannel_callback = NULL;
505 static int vmbus_close_internal(struct vmbus_channel *channel)
507 struct vmbus_channel_close_channel *msg;
511 * process_chn_event(), running in the tasklet, can race
512 * with vmbus_close_internal() in the case of SMP guest, e.g., when
513 * the former is accessing channel->inbound.ring_buffer, the latter
514 * could be freeing the ring_buffer pages.
516 * To resolve the race, we can serialize them by disabling the
517 * tasklet when the latter is running here.
519 hv_event_tasklet_disable(channel);
522 * In case a device driver's probe() fails (e.g.,
523 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
524 * rescinded later (e.g., we dynamically disble an Integrated Service
525 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
526 * here we should skip most of the below cleanup work.
528 if (channel->state != CHANNEL_OPENED_STATE) {
533 channel->state = CHANNEL_OPEN_STATE;
534 channel->sc_creation_callback = NULL;
535 /* Stop callback and cancel the timer asap */
536 if (channel->target_cpu != get_cpu()) {
538 smp_call_function_single(channel->target_cpu, reset_channel_cb,
541 reset_channel_cb(channel);
545 /* Send a closing message */
547 msg = &channel->close_msg.msg;
549 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
550 msg->child_relid = channel->offermsg.child_relid;
552 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
555 pr_err("Close failed: close post msg return is %d\n", ret);
557 * If we failed to post the close msg,
558 * it is perhaps better to leak memory.
563 /* Tear down the gpadl for the channel's ring buffer */
564 if (channel->ringbuffer_gpadlhandle) {
565 ret = vmbus_teardown_gpadl(channel,
566 channel->ringbuffer_gpadlhandle);
568 pr_err("Close failed: teardown gpadl return %d\n", ret);
570 * If we failed to teardown gpadl,
571 * it is perhaps better to leak memory.
577 /* Cleanup the ring buffers for this channel */
578 hv_ringbuffer_cleanup(&channel->outbound);
579 hv_ringbuffer_cleanup(&channel->inbound);
581 free_pages((unsigned long)channel->ringbuffer_pages,
582 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
585 hv_event_tasklet_enable(channel);
591 * vmbus_close - Close the specified channel
593 void vmbus_close(struct vmbus_channel *channel)
595 struct list_head *cur, *tmp;
596 struct vmbus_channel *cur_channel;
598 if (channel->primary_channel != NULL) {
600 * We will only close sub-channels when
601 * the primary is closed.
606 * Close all the sub-channels first and then close the
609 list_for_each_safe(cur, tmp, &channel->sc_list) {
610 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
611 if (cur_channel->state != CHANNEL_OPENED_STATE)
613 vmbus_close_internal(cur_channel);
616 * Now close the primary.
618 vmbus_close_internal(channel);
620 EXPORT_SYMBOL_GPL(vmbus_close);
622 int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
623 u32 bufferlen, u64 requestid,
624 enum vmbus_packet_type type, u32 flags, bool kick_q)
626 struct vmpacket_descriptor desc;
627 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
628 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
629 struct kvec bufferlist[3];
630 u64 aligned_data = 0;
633 bool lock = channel->acquire_ring_lock;
634 int num_vecs = ((bufferlen != 0) ? 3 : 1);
637 /* Setup the descriptor */
638 desc.type = type; /* VmbusPacketTypeDataInBand; */
639 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
640 /* in 8-bytes granularity */
641 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
642 desc.len8 = (u16)(packetlen_aligned >> 3);
643 desc.trans_id = requestid;
645 bufferlist[0].iov_base = &desc;
646 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
647 bufferlist[1].iov_base = buffer;
648 bufferlist[1].iov_len = bufferlen;
649 bufferlist[2].iov_base = &aligned_data;
650 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
652 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, num_vecs,
656 * Signalling the host is conditional on many factors:
657 * 1. The ring state changed from being empty to non-empty.
658 * This is tracked by the variable "signal".
659 * 2. The variable kick_q tracks if more data will be placed
660 * on the ring. We will not signal if more data is
663 * Based on the channel signal state, we will decide
664 * which signaling policy will be applied.
666 * If we cannot write to the ring-buffer; signal the host
667 * even if we may not have written anything. This is a rare
668 * enough condition that it should not matter.
669 * NOTE: in this case, the hvsock channel is an exception, because
670 * it looks the host side's hvsock implementation has a throttling
671 * mechanism which can hurt the performance otherwise.
674 if (channel->signal_policy)
679 if (((ret == 0) && kick_q && signal) ||
680 (ret && !is_hvsock_channel(channel)))
681 vmbus_setevent(channel);
685 EXPORT_SYMBOL(vmbus_sendpacket_ctl);
688 * vmbus_sendpacket() - Send the specified buffer on the given channel
689 * @channel: Pointer to vmbus_channel structure.
690 * @buffer: Pointer to the buffer you want to receive the data into.
691 * @bufferlen: Maximum size of what the the buffer will hold
692 * @requestid: Identifier of the request
693 * @type: Type of packet that is being send e.g. negotiate, time
696 * Sends data in @buffer directly to hyper-v via the vmbus
697 * This will send the data unparsed to hyper-v.
699 * Mainly used by Hyper-V drivers.
701 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
702 u32 bufferlen, u64 requestid,
703 enum vmbus_packet_type type, u32 flags)
705 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
708 EXPORT_SYMBOL(vmbus_sendpacket);
711 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
712 * packets using a GPADL Direct packet type. This interface allows you
713 * to control notifying the host. This will be useful for sending
714 * batched data. Also the sender can control the send flags
717 int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
718 struct hv_page_buffer pagebuffers[],
719 u32 pagecount, void *buffer, u32 bufferlen,
726 struct vmbus_channel_packet_page_buffer desc;
729 u32 packetlen_aligned;
730 struct kvec bufferlist[3];
731 u64 aligned_data = 0;
733 bool lock = channel->acquire_ring_lock;
735 if (pagecount > MAX_PAGE_BUFFER_COUNT)
740 * Adjust the size down since vmbus_channel_packet_page_buffer is the
741 * largest size we support
743 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
744 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
745 sizeof(struct hv_page_buffer));
746 packetlen = descsize + bufferlen;
747 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
749 /* Setup the descriptor */
750 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
752 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
753 desc.length8 = (u16)(packetlen_aligned >> 3);
754 desc.transactionid = requestid;
755 desc.rangecount = pagecount;
757 for (i = 0; i < pagecount; i++) {
758 desc.range[i].len = pagebuffers[i].len;
759 desc.range[i].offset = pagebuffers[i].offset;
760 desc.range[i].pfn = pagebuffers[i].pfn;
763 bufferlist[0].iov_base = &desc;
764 bufferlist[0].iov_len = descsize;
765 bufferlist[1].iov_base = buffer;
766 bufferlist[1].iov_len = bufferlen;
767 bufferlist[2].iov_base = &aligned_data;
768 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
770 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
774 * Signalling the host is conditional on many factors:
775 * 1. The ring state changed from being empty to non-empty.
776 * This is tracked by the variable "signal".
777 * 2. The variable kick_q tracks if more data will be placed
778 * on the ring. We will not signal if more data is
781 * Based on the channel signal state, we will decide
782 * which signaling policy will be applied.
784 * If we cannot write to the ring-buffer; signal the host
785 * even if we may not have written anything. This is a rare
786 * enough condition that it should not matter.
789 if (channel->signal_policy)
794 if (((ret == 0) && kick_q && signal) || (ret))
795 vmbus_setevent(channel);
799 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
802 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
803 * packets using a GPADL Direct packet type.
805 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
806 struct hv_page_buffer pagebuffers[],
807 u32 pagecount, void *buffer, u32 bufferlen,
810 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
811 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
812 buffer, bufferlen, requestid,
816 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
819 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
820 * using a GPADL Direct packet type.
821 * The buffer includes the vmbus descriptor.
823 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
824 struct vmbus_packet_mpb_array *desc,
826 void *buffer, u32 bufferlen, u64 requestid)
830 u32 packetlen_aligned;
831 struct kvec bufferlist[3];
832 u64 aligned_data = 0;
834 bool lock = channel->acquire_ring_lock;
836 packetlen = desc_size + bufferlen;
837 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
839 /* Setup the descriptor */
840 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
841 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
842 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
843 desc->length8 = (u16)(packetlen_aligned >> 3);
844 desc->transactionid = requestid;
845 desc->rangecount = 1;
847 bufferlist[0].iov_base = desc;
848 bufferlist[0].iov_len = desc_size;
849 bufferlist[1].iov_base = buffer;
850 bufferlist[1].iov_len = bufferlen;
851 bufferlist[2].iov_base = &aligned_data;
852 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
854 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
857 if (ret == 0 && signal)
858 vmbus_setevent(channel);
862 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
865 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
866 * using a GPADL Direct packet type.
868 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
869 struct hv_multipage_buffer *multi_pagebuffer,
870 void *buffer, u32 bufferlen, u64 requestid)
873 struct vmbus_channel_packet_multipage_buffer desc;
876 u32 packetlen_aligned;
877 struct kvec bufferlist[3];
878 u64 aligned_data = 0;
880 bool lock = channel->acquire_ring_lock;
881 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
882 multi_pagebuffer->len);
884 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
888 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
889 * the largest size we support
891 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
892 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
894 packetlen = descsize + bufferlen;
895 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
898 /* Setup the descriptor */
899 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
900 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
901 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
902 desc.length8 = (u16)(packetlen_aligned >> 3);
903 desc.transactionid = requestid;
906 desc.range.len = multi_pagebuffer->len;
907 desc.range.offset = multi_pagebuffer->offset;
909 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
910 pfncount * sizeof(u64));
912 bufferlist[0].iov_base = &desc;
913 bufferlist[0].iov_len = descsize;
914 bufferlist[1].iov_base = buffer;
915 bufferlist[1].iov_len = bufferlen;
916 bufferlist[2].iov_base = &aligned_data;
917 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
919 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
922 if (ret == 0 && signal)
923 vmbus_setevent(channel);
927 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
930 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
931 * @channel: Pointer to vmbus_channel structure.
932 * @buffer: Pointer to the buffer you want to receive the data into.
933 * @bufferlen: Maximum size of what the the buffer will hold
934 * @buffer_actual_len: The actual size of the data after it was received
935 * @requestid: Identifier of the request
937 * Receives directly from the hyper-v vmbus and puts the data it received
938 * into Buffer. This will receive the data unparsed from hyper-v.
940 * Mainly used by Hyper-V drivers.
943 __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
944 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
950 ret = hv_ringbuffer_read(&channel->inbound, buffer, bufferlen,
951 buffer_actual_len, requestid, &signal, raw);
954 vmbus_setevent(channel);
959 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
960 u32 bufferlen, u32 *buffer_actual_len,
963 return __vmbus_recvpacket(channel, buffer, bufferlen,
964 buffer_actual_len, requestid, false);
966 EXPORT_SYMBOL(vmbus_recvpacket);
969 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
971 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
972 u32 bufferlen, u32 *buffer_actual_len,
975 return __vmbus_recvpacket(channel, buffer, bufferlen,
976 buffer_actual_len, requestid, true);
978 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);