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
;
46 trace_vmbus_setevent(channel
);
49 * For channels marked as in "low latency" mode
50 * bypass the monitor page mechanism.
52 if (channel
->offermsg
.monitor_allocated
&& !channel
->low_latency
) {
53 vmbus_send_interrupt(channel
->offermsg
.child_relid
);
55 /* Get the child to parent monitor page */
56 monitorpage
= vmbus_connection
.monitor_pages
[1];
58 sync_set_bit(channel
->monitor_bit
,
59 (unsigned long *)&monitorpage
->trigger_group
60 [channel
->monitor_grp
].pending
);
63 vmbus_set_event(channel
);
66 EXPORT_SYMBOL_GPL(vmbus_setevent
);
69 * vmbus_open - Open the specified channel.
71 int vmbus_open(struct vmbus_channel
*newchannel
, u32 send_ringbuffer_size
,
72 u32 recv_ringbuffer_size
, void *userdata
, u32 userdatalen
,
73 void (*onchannelcallback
)(void *context
), void *context
)
75 struct vmbus_channel_open_channel
*open_msg
;
76 struct vmbus_channel_msginfo
*open_info
= NULL
;
81 if (send_ringbuffer_size
% PAGE_SIZE
||
82 recv_ringbuffer_size
% PAGE_SIZE
)
85 spin_lock_irqsave(&newchannel
->lock
, flags
);
86 if (newchannel
->state
== CHANNEL_OPEN_STATE
) {
87 newchannel
->state
= CHANNEL_OPENING_STATE
;
89 spin_unlock_irqrestore(&newchannel
->lock
, flags
);
92 spin_unlock_irqrestore(&newchannel
->lock
, flags
);
94 newchannel
->onchannel_callback
= onchannelcallback
;
95 newchannel
->channel_callback_context
= context
;
97 /* Allocate the ring buffer */
98 page
= alloc_pages_node(cpu_to_node(newchannel
->target_cpu
),
99 GFP_KERNEL
|__GFP_ZERO
,
100 get_order(send_ringbuffer_size
+
101 recv_ringbuffer_size
));
104 page
= alloc_pages(GFP_KERNEL
|__GFP_ZERO
,
105 get_order(send_ringbuffer_size
+
106 recv_ringbuffer_size
));
110 goto error_set_chnstate
;
113 newchannel
->ringbuffer_pages
= page_address(page
);
114 newchannel
->ringbuffer_pagecount
= (send_ringbuffer_size
+
115 recv_ringbuffer_size
) >> PAGE_SHIFT
;
117 ret
= hv_ringbuffer_init(&newchannel
->outbound
, page
,
118 send_ringbuffer_size
>> PAGE_SHIFT
);
122 goto error_free_pages
;
125 ret
= hv_ringbuffer_init(&newchannel
->inbound
,
126 &page
[send_ringbuffer_size
>> PAGE_SHIFT
],
127 recv_ringbuffer_size
>> PAGE_SHIFT
);
130 goto error_free_pages
;
134 /* Establish the gpadl for the ring buffer */
135 newchannel
->ringbuffer_gpadlhandle
= 0;
137 ret
= vmbus_establish_gpadl(newchannel
,
139 send_ringbuffer_size
+
140 recv_ringbuffer_size
,
141 &newchannel
->ringbuffer_gpadlhandle
);
145 goto error_free_pages
;
148 /* Create and init the channel open message */
149 open_info
= kmalloc(sizeof(*open_info
) +
150 sizeof(struct vmbus_channel_open_channel
),
154 goto error_free_gpadl
;
157 init_completion(&open_info
->waitevent
);
158 open_info
->waiting_channel
= newchannel
;
160 open_msg
= (struct vmbus_channel_open_channel
*)open_info
->msg
;
161 open_msg
->header
.msgtype
= CHANNELMSG_OPENCHANNEL
;
162 open_msg
->openid
= newchannel
->offermsg
.child_relid
;
163 open_msg
->child_relid
= newchannel
->offermsg
.child_relid
;
164 open_msg
->ringbuffer_gpadlhandle
= newchannel
->ringbuffer_gpadlhandle
;
165 open_msg
->downstream_ringbuffer_pageoffset
= send_ringbuffer_size
>>
167 open_msg
->target_vp
= newchannel
->target_vp
;
169 if (userdatalen
> MAX_USER_DEFINED_BYTES
) {
171 goto error_free_gpadl
;
175 memcpy(open_msg
->userdata
, userdata
, userdatalen
);
177 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
178 list_add_tail(&open_info
->msglistentry
,
179 &vmbus_connection
.chn_msg_list
);
180 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
182 if (newchannel
->rescind
) {
184 goto error_free_gpadl
;
187 ret
= vmbus_post_msg(open_msg
,
188 sizeof(struct vmbus_channel_open_channel
), true);
190 trace_vmbus_open(open_msg
, ret
);
194 goto error_clean_msglist
;
197 wait_for_completion(&open_info
->waitevent
);
199 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
200 list_del(&open_info
->msglistentry
);
201 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
203 if (newchannel
->rescind
) {
205 goto error_free_gpadl
;
208 if (open_info
->response
.open_result
.status
) {
210 goto error_free_gpadl
;
213 newchannel
->state
= CHANNEL_OPENED_STATE
;
218 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
219 list_del(&open_info
->msglistentry
);
220 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
223 vmbus_teardown_gpadl(newchannel
, newchannel
->ringbuffer_gpadlhandle
);
226 hv_ringbuffer_cleanup(&newchannel
->outbound
);
227 hv_ringbuffer_cleanup(&newchannel
->inbound
);
229 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
231 newchannel
->state
= CHANNEL_OPEN_STATE
;
234 EXPORT_SYMBOL_GPL(vmbus_open
);
236 /* Used for Hyper-V Socket: a guest client's connect() to the host */
237 int vmbus_send_tl_connect_request(const uuid_le
*shv_guest_servie_id
,
238 const uuid_le
*shv_host_servie_id
)
240 struct vmbus_channel_tl_connect_request conn_msg
;
243 memset(&conn_msg
, 0, sizeof(conn_msg
));
244 conn_msg
.header
.msgtype
= CHANNELMSG_TL_CONNECT_REQUEST
;
245 conn_msg
.guest_endpoint_id
= *shv_guest_servie_id
;
246 conn_msg
.host_service_id
= *shv_host_servie_id
;
248 ret
= vmbus_post_msg(&conn_msg
, sizeof(conn_msg
), true);
250 trace_vmbus_send_tl_connect_request(&conn_msg
, ret
);
254 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request
);
257 * create_gpadl_header - Creates a gpadl for the specified buffer
259 static int create_gpadl_header(void *kbuffer
, u32 size
,
260 struct vmbus_channel_msginfo
**msginfo
)
264 struct vmbus_channel_gpadl_header
*gpadl_header
;
265 struct vmbus_channel_gpadl_body
*gpadl_body
;
266 struct vmbus_channel_msginfo
*msgheader
;
267 struct vmbus_channel_msginfo
*msgbody
= NULL
;
270 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
272 pagecount
= size
>> PAGE_SHIFT
;
274 /* do we need a gpadl body msg */
275 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
276 sizeof(struct vmbus_channel_gpadl_header
) -
277 sizeof(struct gpa_range
);
278 pfncount
= pfnsize
/ sizeof(u64
);
280 if (pagecount
> pfncount
) {
281 /* we need a gpadl body */
282 /* fill in the header */
283 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
284 sizeof(struct vmbus_channel_gpadl_header
) +
285 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
286 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
290 INIT_LIST_HEAD(&msgheader
->submsglist
);
291 msgheader
->msgsize
= msgsize
;
293 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
295 gpadl_header
->rangecount
= 1;
296 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
297 pagecount
* sizeof(u64
);
298 gpadl_header
->range
[0].byte_offset
= 0;
299 gpadl_header
->range
[0].byte_count
= size
;
300 for (i
= 0; i
< pfncount
; i
++)
301 gpadl_header
->range
[0].pfn_array
[i
] = slow_virt_to_phys(
302 kbuffer
+ PAGE_SIZE
* i
) >> PAGE_SHIFT
;
303 *msginfo
= msgheader
;
306 pfnleft
= pagecount
- pfncount
;
308 /* how many pfns can we fit */
309 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
310 sizeof(struct vmbus_channel_gpadl_body
);
311 pfncount
= pfnsize
/ sizeof(u64
);
313 /* fill in the body */
315 if (pfnleft
> pfncount
)
320 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
321 sizeof(struct vmbus_channel_gpadl_body
) +
322 pfncurr
* sizeof(u64
);
323 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
326 struct vmbus_channel_msginfo
*pos
= NULL
;
327 struct vmbus_channel_msginfo
*tmp
= NULL
;
329 * Free up all the allocated messages.
331 list_for_each_entry_safe(pos
, tmp
,
332 &msgheader
->submsglist
,
335 list_del(&pos
->msglistentry
);
342 msgbody
->msgsize
= msgsize
;
344 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
347 * Gpadl is u32 and we are using a pointer which could
349 * This is governed by the guest/host protocol and
350 * so the hypervisor guarantees that this is ok.
352 for (i
= 0; i
< pfncurr
; i
++)
353 gpadl_body
->pfn
[i
] = slow_virt_to_phys(
354 kbuffer
+ PAGE_SIZE
* (pfnsum
+ i
)) >>
357 /* add to msg header */
358 list_add_tail(&msgbody
->msglistentry
,
359 &msgheader
->submsglist
);
364 /* everything fits in a header */
365 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
366 sizeof(struct vmbus_channel_gpadl_header
) +
367 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
368 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
369 if (msgheader
== NULL
)
372 INIT_LIST_HEAD(&msgheader
->submsglist
);
373 msgheader
->msgsize
= msgsize
;
375 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
377 gpadl_header
->rangecount
= 1;
378 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
379 pagecount
* sizeof(u64
);
380 gpadl_header
->range
[0].byte_offset
= 0;
381 gpadl_header
->range
[0].byte_count
= size
;
382 for (i
= 0; i
< pagecount
; i
++)
383 gpadl_header
->range
[0].pfn_array
[i
] = slow_virt_to_phys(
384 kbuffer
+ PAGE_SIZE
* i
) >> PAGE_SHIFT
;
386 *msginfo
= msgheader
;
397 * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
399 * @channel: a channel
400 * @kbuffer: from kmalloc or vmalloc
401 * @size: page-size multiple
402 * @gpadl_handle: some funky thing
404 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
405 u32 size
, u32
*gpadl_handle
)
407 struct vmbus_channel_gpadl_header
*gpadlmsg
;
408 struct vmbus_channel_gpadl_body
*gpadl_body
;
409 struct vmbus_channel_msginfo
*msginfo
= NULL
;
410 struct vmbus_channel_msginfo
*submsginfo
, *tmp
;
411 struct list_head
*curr
;
412 u32 next_gpadl_handle
;
417 (atomic_inc_return(&vmbus_connection
.next_gpadl_handle
) - 1);
419 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
);
423 init_completion(&msginfo
->waitevent
);
424 msginfo
->waiting_channel
= channel
;
426 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
427 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
428 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
429 gpadlmsg
->gpadl
= next_gpadl_handle
;
432 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
433 list_add_tail(&msginfo
->msglistentry
,
434 &vmbus_connection
.chn_msg_list
);
436 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
438 if (channel
->rescind
) {
443 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
444 sizeof(*msginfo
), true);
446 trace_vmbus_establish_gpadl_header(gpadlmsg
, ret
);
451 list_for_each(curr
, &msginfo
->submsglist
) {
452 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
454 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
456 gpadl_body
->header
.msgtype
=
457 CHANNELMSG_GPADL_BODY
;
458 gpadl_body
->gpadl
= next_gpadl_handle
;
460 ret
= vmbus_post_msg(gpadl_body
,
461 submsginfo
->msgsize
- sizeof(*submsginfo
),
464 trace_vmbus_establish_gpadl_body(gpadl_body
, ret
);
470 wait_for_completion(&msginfo
->waitevent
);
472 if (channel
->rescind
) {
477 /* At this point, we received the gpadl created msg */
478 *gpadl_handle
= gpadlmsg
->gpadl
;
481 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
482 list_del(&msginfo
->msglistentry
);
483 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
484 list_for_each_entry_safe(submsginfo
, tmp
, &msginfo
->submsglist
,
492 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
495 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
497 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
499 struct vmbus_channel_gpadl_teardown
*msg
;
500 struct vmbus_channel_msginfo
*info
;
504 info
= kmalloc(sizeof(*info
) +
505 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
509 init_completion(&info
->waitevent
);
510 info
->waiting_channel
= channel
;
512 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
514 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
515 msg
->child_relid
= channel
->offermsg
.child_relid
;
516 msg
->gpadl
= gpadl_handle
;
518 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
519 list_add_tail(&info
->msglistentry
,
520 &vmbus_connection
.chn_msg_list
);
521 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
523 if (channel
->rescind
)
526 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_gpadl_teardown
),
529 trace_vmbus_teardown_gpadl(msg
, ret
);
534 wait_for_completion(&info
->waitevent
);
538 * If the channel has been rescinded;
539 * we will be awakened by the rescind
540 * handler; set the error code to zero so we don't leak memory.
542 if (channel
->rescind
)
545 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
546 list_del(&info
->msglistentry
);
547 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
552 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
554 static void reset_channel_cb(void *arg
)
556 struct vmbus_channel
*channel
= arg
;
558 channel
->onchannel_callback
= NULL
;
561 void vmbus_reset_channel_cb(struct vmbus_channel
*channel
)
564 * vmbus_on_event(), running in the per-channel tasklet, can race
565 * with vmbus_close_internal() in the case of SMP guest, e.g., when
566 * the former is accessing channel->inbound.ring_buffer, the latter
567 * could be freeing the ring_buffer pages, so here we must stop it
570 tasklet_disable(&channel
->callback_event
);
572 channel
->sc_creation_callback
= NULL
;
574 /* Stop the callback asap */
575 if (channel
->target_cpu
!= get_cpu()) {
577 smp_call_function_single(channel
->target_cpu
, reset_channel_cb
,
580 reset_channel_cb(channel
);
584 /* Re-enable tasklet for use on re-open */
585 tasklet_enable(&channel
->callback_event
);
588 static int vmbus_close_internal(struct vmbus_channel
*channel
)
590 struct vmbus_channel_close_channel
*msg
;
593 vmbus_reset_channel_cb(channel
);
596 * In case a device driver's probe() fails (e.g.,
597 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
598 * rescinded later (e.g., we dynamically disable an Integrated Service
599 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
600 * here we should skip most of the below cleanup work.
602 if (channel
->state
!= CHANNEL_OPENED_STATE
) {
607 channel
->state
= CHANNEL_OPEN_STATE
;
609 /* Send a closing message */
611 msg
= &channel
->close_msg
.msg
;
613 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
614 msg
->child_relid
= channel
->offermsg
.child_relid
;
616 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
),
619 trace_vmbus_close_internal(msg
, ret
);
622 pr_err("Close failed: close post msg return is %d\n", ret
);
624 * If we failed to post the close msg,
625 * it is perhaps better to leak memory.
630 /* Tear down the gpadl for the channel's ring buffer */
631 if (channel
->ringbuffer_gpadlhandle
) {
632 ret
= vmbus_teardown_gpadl(channel
,
633 channel
->ringbuffer_gpadlhandle
);
635 pr_err("Close failed: teardown gpadl return %d\n", ret
);
637 * If we failed to teardown gpadl,
638 * it is perhaps better to leak memory.
644 /* Cleanup the ring buffers for this channel */
645 hv_ringbuffer_cleanup(&channel
->outbound
);
646 hv_ringbuffer_cleanup(&channel
->inbound
);
648 free_pages((unsigned long)channel
->ringbuffer_pages
,
649 get_order(channel
->ringbuffer_pagecount
* PAGE_SIZE
));
656 * vmbus_close - Close the specified channel
658 void vmbus_close(struct vmbus_channel
*channel
)
660 struct list_head
*cur
, *tmp
;
661 struct vmbus_channel
*cur_channel
;
663 if (channel
->primary_channel
!= NULL
) {
665 * We will only close sub-channels when
666 * the primary is closed.
671 * Close all the sub-channels first and then close the
674 list_for_each_safe(cur
, tmp
, &channel
->sc_list
) {
675 cur_channel
= list_entry(cur
, struct vmbus_channel
, sc_list
);
676 if (cur_channel
->rescind
) {
677 wait_for_completion(&cur_channel
->rescind_event
);
678 mutex_lock(&vmbus_connection
.channel_mutex
);
679 vmbus_close_internal(cur_channel
);
680 hv_process_channel_removal(
681 cur_channel
->offermsg
.child_relid
);
683 mutex_lock(&vmbus_connection
.channel_mutex
);
684 vmbus_close_internal(cur_channel
);
686 mutex_unlock(&vmbus_connection
.channel_mutex
);
689 * Now close the primary.
691 mutex_lock(&vmbus_connection
.channel_mutex
);
692 vmbus_close_internal(channel
);
693 mutex_unlock(&vmbus_connection
.channel_mutex
);
695 EXPORT_SYMBOL_GPL(vmbus_close
);
698 * vmbus_sendpacket() - Send the specified buffer on the given channel
699 * @channel: Pointer to vmbus_channel structure.
700 * @buffer: Pointer to the buffer you want to receive the data into.
701 * @bufferlen: Maximum size of what the the buffer will hold
702 * @requestid: Identifier of the request
703 * @type: Type of packet that is being send e.g. negotiate, time
706 * Sends data in @buffer directly to hyper-v via the vmbus
707 * This will send the data unparsed to hyper-v.
709 * Mainly used by Hyper-V drivers.
711 int vmbus_sendpacket(struct vmbus_channel
*channel
, void *buffer
,
712 u32 bufferlen
, u64 requestid
,
713 enum vmbus_packet_type type
, u32 flags
)
715 struct vmpacket_descriptor desc
;
716 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
717 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
718 struct kvec bufferlist
[3];
719 u64 aligned_data
= 0;
720 int num_vecs
= ((bufferlen
!= 0) ? 3 : 1);
723 /* Setup the descriptor */
724 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
725 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
726 /* in 8-bytes granularity */
727 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
728 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
729 desc
.trans_id
= requestid
;
731 bufferlist
[0].iov_base
= &desc
;
732 bufferlist
[0].iov_len
= sizeof(struct vmpacket_descriptor
);
733 bufferlist
[1].iov_base
= buffer
;
734 bufferlist
[1].iov_len
= bufferlen
;
735 bufferlist
[2].iov_base
= &aligned_data
;
736 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
738 return hv_ringbuffer_write(channel
, bufferlist
, num_vecs
);
740 EXPORT_SYMBOL(vmbus_sendpacket
);
743 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
744 * packets using a GPADL Direct packet type. This interface allows you
745 * to control notifying the host. This will be useful for sending
746 * batched data. Also the sender can control the send flags
749 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
750 struct hv_page_buffer pagebuffers
[],
751 u32 pagecount
, void *buffer
, u32 bufferlen
,
755 struct vmbus_channel_packet_page_buffer desc
;
758 u32 packetlen_aligned
;
759 struct kvec bufferlist
[3];
760 u64 aligned_data
= 0;
762 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
766 * Adjust the size down since vmbus_channel_packet_page_buffer is the
767 * largest size we support
769 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
770 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
771 sizeof(struct hv_page_buffer
));
772 packetlen
= descsize
+ bufferlen
;
773 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
775 /* Setup the descriptor */
776 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
777 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
778 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes granularity */
779 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
780 desc
.transactionid
= requestid
;
782 desc
.rangecount
= pagecount
;
784 for (i
= 0; i
< pagecount
; i
++) {
785 desc
.range
[i
].len
= pagebuffers
[i
].len
;
786 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
787 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
790 bufferlist
[0].iov_base
= &desc
;
791 bufferlist
[0].iov_len
= descsize
;
792 bufferlist
[1].iov_base
= buffer
;
793 bufferlist
[1].iov_len
= bufferlen
;
794 bufferlist
[2].iov_base
= &aligned_data
;
795 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
797 return hv_ringbuffer_write(channel
, bufferlist
, 3);
799 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
802 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
803 * using a GPADL Direct packet type.
804 * The buffer includes the vmbus descriptor.
806 int vmbus_sendpacket_mpb_desc(struct vmbus_channel
*channel
,
807 struct vmbus_packet_mpb_array
*desc
,
809 void *buffer
, u32 bufferlen
, u64 requestid
)
812 u32 packetlen_aligned
;
813 struct kvec bufferlist
[3];
814 u64 aligned_data
= 0;
816 packetlen
= desc_size
+ bufferlen
;
817 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
819 /* Setup the descriptor */
820 desc
->type
= VM_PKT_DATA_USING_GPA_DIRECT
;
821 desc
->flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
822 desc
->dataoffset8
= desc_size
>> 3; /* in 8-bytes granularity */
823 desc
->length8
= (u16
)(packetlen_aligned
>> 3);
824 desc
->transactionid
= requestid
;
826 desc
->rangecount
= 1;
828 bufferlist
[0].iov_base
= desc
;
829 bufferlist
[0].iov_len
= desc_size
;
830 bufferlist
[1].iov_base
= buffer
;
831 bufferlist
[1].iov_len
= bufferlen
;
832 bufferlist
[2].iov_base
= &aligned_data
;
833 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
835 return hv_ringbuffer_write(channel
, bufferlist
, 3);
837 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc
);
840 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
841 * @channel: Pointer to vmbus_channel structure.
842 * @buffer: Pointer to the buffer you want to receive the data into.
843 * @bufferlen: Maximum size of what the the buffer will hold
844 * @buffer_actual_len: The actual size of the data after it was received
845 * @requestid: Identifier of the request
847 * Receives directly from the hyper-v vmbus and puts the data it received
848 * into Buffer. This will receive the data unparsed from hyper-v.
850 * Mainly used by Hyper-V drivers.
853 __vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
854 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
,
857 return hv_ringbuffer_read(channel
, buffer
, bufferlen
,
858 buffer_actual_len
, requestid
, raw
);
862 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
863 u32 bufferlen
, u32
*buffer_actual_len
,
866 return __vmbus_recvpacket(channel
, buffer
, bufferlen
,
867 buffer_actual_len
, requestid
, false);
869 EXPORT_SYMBOL(vmbus_recvpacket
);
872 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
874 int vmbus_recvpacket_raw(struct vmbus_channel
*channel
, void *buffer
,
875 u32 bufferlen
, u32
*buffer_actual_len
,
878 return __vmbus_recvpacket(channel
, buffer
, bufferlen
,
879 buffer_actual_len
, requestid
, true);
881 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);