1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Microsoft Corporation.
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/wait.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/hyperv.h>
18 #include <linux/uio.h>
19 #include <linux/interrupt.h>
22 #include "hyperv_vmbus.h"
24 #define NUM_PAGES_SPANNED(addr, len) \
25 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
27 static unsigned long virt_to_hvpfn(void *addr
)
31 if (is_vmalloc_addr(addr
))
32 paddr
= page_to_phys(vmalloc_to_page(addr
)) +
37 return paddr
>> PAGE_SHIFT
;
41 * vmbus_setevent- Trigger an event notification on the specified
44 void vmbus_setevent(struct vmbus_channel
*channel
)
46 struct hv_monitor_page
*monitorpage
;
48 trace_vmbus_setevent(channel
);
51 * For channels marked as in "low latency" mode
52 * bypass the monitor page mechanism.
54 if (channel
->offermsg
.monitor_allocated
&& !channel
->low_latency
) {
55 vmbus_send_interrupt(channel
->offermsg
.child_relid
);
57 /* Get the child to parent monitor page */
58 monitorpage
= vmbus_connection
.monitor_pages
[1];
60 sync_set_bit(channel
->monitor_bit
,
61 (unsigned long *)&monitorpage
->trigger_group
62 [channel
->monitor_grp
].pending
);
65 vmbus_set_event(channel
);
68 EXPORT_SYMBOL_GPL(vmbus_setevent
);
70 /* vmbus_free_ring - drop mapping of ring buffer */
71 void vmbus_free_ring(struct vmbus_channel
*channel
)
73 hv_ringbuffer_cleanup(&channel
->outbound
);
74 hv_ringbuffer_cleanup(&channel
->inbound
);
76 if (channel
->ringbuffer_page
) {
77 __free_pages(channel
->ringbuffer_page
,
78 get_order(channel
->ringbuffer_pagecount
80 channel
->ringbuffer_page
= NULL
;
83 EXPORT_SYMBOL_GPL(vmbus_free_ring
);
85 /* vmbus_alloc_ring - allocate and map pages for ring buffer */
86 int vmbus_alloc_ring(struct vmbus_channel
*newchannel
,
87 u32 send_size
, u32 recv_size
)
92 if (send_size
% PAGE_SIZE
|| recv_size
% PAGE_SIZE
)
95 /* Allocate the ring buffer */
96 order
= get_order(send_size
+ recv_size
);
97 page
= alloc_pages_node(cpu_to_node(newchannel
->target_cpu
),
98 GFP_KERNEL
|__GFP_ZERO
, order
);
101 page
= alloc_pages(GFP_KERNEL
|__GFP_ZERO
, order
);
106 newchannel
->ringbuffer_page
= page
;
107 newchannel
->ringbuffer_pagecount
= (send_size
+ recv_size
) >> PAGE_SHIFT
;
108 newchannel
->ringbuffer_send_offset
= send_size
>> PAGE_SHIFT
;
112 EXPORT_SYMBOL_GPL(vmbus_alloc_ring
);
114 static int __vmbus_open(struct vmbus_channel
*newchannel
,
115 void *userdata
, u32 userdatalen
,
116 void (*onchannelcallback
)(void *context
), void *context
)
118 struct vmbus_channel_open_channel
*open_msg
;
119 struct vmbus_channel_msginfo
*open_info
= NULL
;
120 struct page
*page
= newchannel
->ringbuffer_page
;
121 u32 send_pages
, recv_pages
;
125 if (userdatalen
> MAX_USER_DEFINED_BYTES
)
128 send_pages
= newchannel
->ringbuffer_send_offset
;
129 recv_pages
= newchannel
->ringbuffer_pagecount
- send_pages
;
131 spin_lock_irqsave(&newchannel
->lock
, flags
);
132 if (newchannel
->state
!= CHANNEL_OPEN_STATE
) {
133 spin_unlock_irqrestore(&newchannel
->lock
, flags
);
136 spin_unlock_irqrestore(&newchannel
->lock
, flags
);
138 newchannel
->state
= CHANNEL_OPENING_STATE
;
139 newchannel
->onchannel_callback
= onchannelcallback
;
140 newchannel
->channel_callback_context
= context
;
142 err
= hv_ringbuffer_init(&newchannel
->outbound
, page
, send_pages
);
144 goto error_clean_ring
;
146 err
= hv_ringbuffer_init(&newchannel
->inbound
,
147 &page
[send_pages
], recv_pages
);
149 goto error_clean_ring
;
151 /* Establish the gpadl for the ring buffer */
152 newchannel
->ringbuffer_gpadlhandle
= 0;
154 err
= vmbus_establish_gpadl(newchannel
,
155 page_address(newchannel
->ringbuffer_page
),
156 (send_pages
+ recv_pages
) << PAGE_SHIFT
,
157 &newchannel
->ringbuffer_gpadlhandle
);
159 goto error_clean_ring
;
161 /* Create and init the channel open message */
162 open_info
= kmalloc(sizeof(*open_info
) +
163 sizeof(struct vmbus_channel_open_channel
),
167 goto error_free_gpadl
;
170 init_completion(&open_info
->waitevent
);
171 open_info
->waiting_channel
= newchannel
;
173 open_msg
= (struct vmbus_channel_open_channel
*)open_info
->msg
;
174 open_msg
->header
.msgtype
= CHANNELMSG_OPENCHANNEL
;
175 open_msg
->openid
= newchannel
->offermsg
.child_relid
;
176 open_msg
->child_relid
= newchannel
->offermsg
.child_relid
;
177 open_msg
->ringbuffer_gpadlhandle
= newchannel
->ringbuffer_gpadlhandle
;
178 open_msg
->downstream_ringbuffer_pageoffset
= newchannel
->ringbuffer_send_offset
;
179 open_msg
->target_vp
= newchannel
->target_vp
;
182 memcpy(open_msg
->userdata
, userdata
, userdatalen
);
184 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
185 list_add_tail(&open_info
->msglistentry
,
186 &vmbus_connection
.chn_msg_list
);
187 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
189 if (newchannel
->rescind
) {
191 goto error_free_info
;
194 err
= vmbus_post_msg(open_msg
,
195 sizeof(struct vmbus_channel_open_channel
), true);
197 trace_vmbus_open(open_msg
, err
);
200 goto error_clean_msglist
;
202 wait_for_completion(&open_info
->waitevent
);
204 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
205 list_del(&open_info
->msglistentry
);
206 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
208 if (newchannel
->rescind
) {
210 goto error_free_info
;
213 if (open_info
->response
.open_result
.status
) {
215 goto error_free_info
;
218 newchannel
->state
= CHANNEL_OPENED_STATE
;
223 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
224 list_del(&open_info
->msglistentry
);
225 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
229 vmbus_teardown_gpadl(newchannel
, newchannel
->ringbuffer_gpadlhandle
);
230 newchannel
->ringbuffer_gpadlhandle
= 0;
232 hv_ringbuffer_cleanup(&newchannel
->outbound
);
233 hv_ringbuffer_cleanup(&newchannel
->inbound
);
234 newchannel
->state
= CHANNEL_OPEN_STATE
;
239 * vmbus_connect_ring - Open the channel but reuse ring buffer
241 int vmbus_connect_ring(struct vmbus_channel
*newchannel
,
242 void (*onchannelcallback
)(void *context
), void *context
)
244 return __vmbus_open(newchannel
, NULL
, 0, onchannelcallback
, context
);
246 EXPORT_SYMBOL_GPL(vmbus_connect_ring
);
249 * vmbus_open - Open the specified channel.
251 int vmbus_open(struct vmbus_channel
*newchannel
,
252 u32 send_ringbuffer_size
, u32 recv_ringbuffer_size
,
253 void *userdata
, u32 userdatalen
,
254 void (*onchannelcallback
)(void *context
), void *context
)
258 err
= vmbus_alloc_ring(newchannel
, send_ringbuffer_size
,
259 recv_ringbuffer_size
);
263 err
= __vmbus_open(newchannel
, userdata
, userdatalen
,
264 onchannelcallback
, context
);
266 vmbus_free_ring(newchannel
);
270 EXPORT_SYMBOL_GPL(vmbus_open
);
272 /* Used for Hyper-V Socket: a guest client's connect() to the host */
273 int vmbus_send_tl_connect_request(const guid_t
*shv_guest_servie_id
,
274 const guid_t
*shv_host_servie_id
)
276 struct vmbus_channel_tl_connect_request conn_msg
;
279 memset(&conn_msg
, 0, sizeof(conn_msg
));
280 conn_msg
.header
.msgtype
= CHANNELMSG_TL_CONNECT_REQUEST
;
281 conn_msg
.guest_endpoint_id
= *shv_guest_servie_id
;
282 conn_msg
.host_service_id
= *shv_host_servie_id
;
284 ret
= vmbus_post_msg(&conn_msg
, sizeof(conn_msg
), true);
286 trace_vmbus_send_tl_connect_request(&conn_msg
, ret
);
290 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request
);
293 * create_gpadl_header - Creates a gpadl for the specified buffer
295 static int create_gpadl_header(void *kbuffer
, u32 size
,
296 struct vmbus_channel_msginfo
**msginfo
)
300 struct vmbus_channel_gpadl_header
*gpadl_header
;
301 struct vmbus_channel_gpadl_body
*gpadl_body
;
302 struct vmbus_channel_msginfo
*msgheader
;
303 struct vmbus_channel_msginfo
*msgbody
= NULL
;
306 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
308 pagecount
= size
>> PAGE_SHIFT
;
310 /* do we need a gpadl body msg */
311 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
312 sizeof(struct vmbus_channel_gpadl_header
) -
313 sizeof(struct gpa_range
);
314 pfncount
= pfnsize
/ sizeof(u64
);
316 if (pagecount
> pfncount
) {
317 /* we need a gpadl body */
318 /* fill in the header */
319 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
320 sizeof(struct vmbus_channel_gpadl_header
) +
321 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
322 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
326 INIT_LIST_HEAD(&msgheader
->submsglist
);
327 msgheader
->msgsize
= msgsize
;
329 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
331 gpadl_header
->rangecount
= 1;
332 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
333 pagecount
* sizeof(u64
);
334 gpadl_header
->range
[0].byte_offset
= 0;
335 gpadl_header
->range
[0].byte_count
= size
;
336 for (i
= 0; i
< pfncount
; i
++)
337 gpadl_header
->range
[0].pfn_array
[i
] = virt_to_hvpfn(
338 kbuffer
+ PAGE_SIZE
* i
);
339 *msginfo
= msgheader
;
342 pfnleft
= pagecount
- pfncount
;
344 /* how many pfns can we fit */
345 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
346 sizeof(struct vmbus_channel_gpadl_body
);
347 pfncount
= pfnsize
/ sizeof(u64
);
349 /* fill in the body */
351 if (pfnleft
> pfncount
)
356 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
357 sizeof(struct vmbus_channel_gpadl_body
) +
358 pfncurr
* sizeof(u64
);
359 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
362 struct vmbus_channel_msginfo
*pos
= NULL
;
363 struct vmbus_channel_msginfo
*tmp
= NULL
;
365 * Free up all the allocated messages.
367 list_for_each_entry_safe(pos
, tmp
,
368 &msgheader
->submsglist
,
371 list_del(&pos
->msglistentry
);
378 msgbody
->msgsize
= msgsize
;
380 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
383 * Gpadl is u32 and we are using a pointer which could
385 * This is governed by the guest/host protocol and
386 * so the hypervisor guarantees that this is ok.
388 for (i
= 0; i
< pfncurr
; i
++)
389 gpadl_body
->pfn
[i
] = virt_to_hvpfn(
390 kbuffer
+ PAGE_SIZE
* (pfnsum
+ i
));
392 /* add to msg header */
393 list_add_tail(&msgbody
->msglistentry
,
394 &msgheader
->submsglist
);
399 /* everything fits in a header */
400 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
401 sizeof(struct vmbus_channel_gpadl_header
) +
402 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
403 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
404 if (msgheader
== NULL
)
407 INIT_LIST_HEAD(&msgheader
->submsglist
);
408 msgheader
->msgsize
= msgsize
;
410 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
412 gpadl_header
->rangecount
= 1;
413 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
414 pagecount
* sizeof(u64
);
415 gpadl_header
->range
[0].byte_offset
= 0;
416 gpadl_header
->range
[0].byte_count
= size
;
417 for (i
= 0; i
< pagecount
; i
++)
418 gpadl_header
->range
[0].pfn_array
[i
] = virt_to_hvpfn(
419 kbuffer
+ PAGE_SIZE
* i
);
421 *msginfo
= msgheader
;
432 * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
434 * @channel: a channel
435 * @kbuffer: from kmalloc or vmalloc
436 * @size: page-size multiple
437 * @gpadl_handle: some funky thing
439 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
440 u32 size
, u32
*gpadl_handle
)
442 struct vmbus_channel_gpadl_header
*gpadlmsg
;
443 struct vmbus_channel_gpadl_body
*gpadl_body
;
444 struct vmbus_channel_msginfo
*msginfo
= NULL
;
445 struct vmbus_channel_msginfo
*submsginfo
, *tmp
;
446 struct list_head
*curr
;
447 u32 next_gpadl_handle
;
452 (atomic_inc_return(&vmbus_connection
.next_gpadl_handle
) - 1);
454 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
);
458 init_completion(&msginfo
->waitevent
);
459 msginfo
->waiting_channel
= channel
;
461 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
462 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
463 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
464 gpadlmsg
->gpadl
= next_gpadl_handle
;
467 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
468 list_add_tail(&msginfo
->msglistentry
,
469 &vmbus_connection
.chn_msg_list
);
471 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
473 if (channel
->rescind
) {
478 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
479 sizeof(*msginfo
), true);
481 trace_vmbus_establish_gpadl_header(gpadlmsg
, ret
);
486 list_for_each(curr
, &msginfo
->submsglist
) {
487 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
489 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
491 gpadl_body
->header
.msgtype
=
492 CHANNELMSG_GPADL_BODY
;
493 gpadl_body
->gpadl
= next_gpadl_handle
;
495 ret
= vmbus_post_msg(gpadl_body
,
496 submsginfo
->msgsize
- sizeof(*submsginfo
),
499 trace_vmbus_establish_gpadl_body(gpadl_body
, ret
);
505 wait_for_completion(&msginfo
->waitevent
);
507 if (msginfo
->response
.gpadl_created
.creation_status
!= 0) {
508 pr_err("Failed to establish GPADL: err = 0x%x\n",
509 msginfo
->response
.gpadl_created
.creation_status
);
515 if (channel
->rescind
) {
520 /* At this point, we received the gpadl created msg */
521 *gpadl_handle
= gpadlmsg
->gpadl
;
524 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
525 list_del(&msginfo
->msglistentry
);
526 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
527 list_for_each_entry_safe(submsginfo
, tmp
, &msginfo
->submsglist
,
535 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
538 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
540 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
542 struct vmbus_channel_gpadl_teardown
*msg
;
543 struct vmbus_channel_msginfo
*info
;
547 info
= kmalloc(sizeof(*info
) +
548 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
552 init_completion(&info
->waitevent
);
553 info
->waiting_channel
= channel
;
555 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
557 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
558 msg
->child_relid
= channel
->offermsg
.child_relid
;
559 msg
->gpadl
= gpadl_handle
;
561 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
562 list_add_tail(&info
->msglistentry
,
563 &vmbus_connection
.chn_msg_list
);
564 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
566 if (channel
->rescind
)
569 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_gpadl_teardown
),
572 trace_vmbus_teardown_gpadl(msg
, ret
);
577 wait_for_completion(&info
->waitevent
);
581 * If the channel has been rescinded;
582 * we will be awakened by the rescind
583 * handler; set the error code to zero so we don't leak memory.
585 if (channel
->rescind
)
588 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
589 list_del(&info
->msglistentry
);
590 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
595 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
597 static void reset_channel_cb(void *arg
)
599 struct vmbus_channel
*channel
= arg
;
601 channel
->onchannel_callback
= NULL
;
604 void vmbus_reset_channel_cb(struct vmbus_channel
*channel
)
607 * vmbus_on_event(), running in the per-channel tasklet, can race
608 * with vmbus_close_internal() in the case of SMP guest, e.g., when
609 * the former is accessing channel->inbound.ring_buffer, the latter
610 * could be freeing the ring_buffer pages, so here we must stop it
613 tasklet_disable(&channel
->callback_event
);
615 channel
->sc_creation_callback
= NULL
;
617 /* Stop the callback asap */
618 if (channel
->target_cpu
!= get_cpu()) {
620 smp_call_function_single(channel
->target_cpu
, reset_channel_cb
,
623 reset_channel_cb(channel
);
627 /* Re-enable tasklet for use on re-open */
628 tasklet_enable(&channel
->callback_event
);
631 static int vmbus_close_internal(struct vmbus_channel
*channel
)
633 struct vmbus_channel_close_channel
*msg
;
636 vmbus_reset_channel_cb(channel
);
639 * In case a device driver's probe() fails (e.g.,
640 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
641 * rescinded later (e.g., we dynamically disable an Integrated Service
642 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
643 * here we should skip most of the below cleanup work.
645 if (channel
->state
!= CHANNEL_OPENED_STATE
)
648 channel
->state
= CHANNEL_OPEN_STATE
;
650 /* Send a closing message */
652 msg
= &channel
->close_msg
.msg
;
654 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
655 msg
->child_relid
= channel
->offermsg
.child_relid
;
657 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
),
660 trace_vmbus_close_internal(msg
, ret
);
663 pr_err("Close failed: close post msg return is %d\n", ret
);
665 * If we failed to post the close msg,
666 * it is perhaps better to leak memory.
670 /* Tear down the gpadl for the channel's ring buffer */
671 else if (channel
->ringbuffer_gpadlhandle
) {
672 ret
= vmbus_teardown_gpadl(channel
,
673 channel
->ringbuffer_gpadlhandle
);
675 pr_err("Close failed: teardown gpadl return %d\n", ret
);
677 * If we failed to teardown gpadl,
678 * it is perhaps better to leak memory.
682 channel
->ringbuffer_gpadlhandle
= 0;
688 /* disconnect ring - close all channels */
689 int vmbus_disconnect_ring(struct vmbus_channel
*channel
)
691 struct vmbus_channel
*cur_channel
, *tmp
;
694 if (channel
->primary_channel
!= NULL
)
697 list_for_each_entry_safe(cur_channel
, tmp
, &channel
->sc_list
, sc_list
) {
698 if (cur_channel
->rescind
)
699 wait_for_completion(&cur_channel
->rescind_event
);
701 mutex_lock(&vmbus_connection
.channel_mutex
);
702 if (vmbus_close_internal(cur_channel
) == 0) {
703 vmbus_free_ring(cur_channel
);
705 if (cur_channel
->rescind
)
706 hv_process_channel_removal(cur_channel
);
708 mutex_unlock(&vmbus_connection
.channel_mutex
);
712 * Now close the primary.
714 mutex_lock(&vmbus_connection
.channel_mutex
);
715 ret
= vmbus_close_internal(channel
);
716 mutex_unlock(&vmbus_connection
.channel_mutex
);
720 EXPORT_SYMBOL_GPL(vmbus_disconnect_ring
);
723 * vmbus_close - Close the specified channel
725 void vmbus_close(struct vmbus_channel
*channel
)
727 if (vmbus_disconnect_ring(channel
) == 0)
728 vmbus_free_ring(channel
);
730 EXPORT_SYMBOL_GPL(vmbus_close
);
733 * vmbus_sendpacket() - Send the specified buffer on the given channel
734 * @channel: Pointer to vmbus_channel structure
735 * @buffer: Pointer to the buffer you want to send the data from.
736 * @bufferlen: Maximum size of what the buffer holds.
737 * @requestid: Identifier of the request
738 * @type: Type of packet that is being sent e.g. negotiate, time
740 * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
742 * Sends data in @buffer directly to Hyper-V via the vmbus.
743 * This will send the data unparsed to Hyper-V.
745 * Mainly used by Hyper-V drivers.
747 int vmbus_sendpacket(struct vmbus_channel
*channel
, void *buffer
,
748 u32 bufferlen
, u64 requestid
,
749 enum vmbus_packet_type type
, u32 flags
)
751 struct vmpacket_descriptor desc
;
752 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
753 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
754 struct kvec bufferlist
[3];
755 u64 aligned_data
= 0;
756 int num_vecs
= ((bufferlen
!= 0) ? 3 : 1);
759 /* Setup the descriptor */
760 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
761 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
762 /* in 8-bytes granularity */
763 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
764 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
765 desc
.trans_id
= requestid
;
767 bufferlist
[0].iov_base
= &desc
;
768 bufferlist
[0].iov_len
= sizeof(struct vmpacket_descriptor
);
769 bufferlist
[1].iov_base
= buffer
;
770 bufferlist
[1].iov_len
= bufferlen
;
771 bufferlist
[2].iov_base
= &aligned_data
;
772 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
774 return hv_ringbuffer_write(channel
, bufferlist
, num_vecs
);
776 EXPORT_SYMBOL(vmbus_sendpacket
);
779 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
780 * packets using a GPADL Direct packet type. This interface allows you
781 * to control notifying the host. This will be useful for sending
782 * batched data. Also the sender can control the send flags
785 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
786 struct hv_page_buffer pagebuffers
[],
787 u32 pagecount
, void *buffer
, u32 bufferlen
,
791 struct vmbus_channel_packet_page_buffer desc
;
794 u32 packetlen_aligned
;
795 struct kvec bufferlist
[3];
796 u64 aligned_data
= 0;
798 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
802 * Adjust the size down since vmbus_channel_packet_page_buffer is the
803 * largest size we support
805 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
806 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
807 sizeof(struct hv_page_buffer
));
808 packetlen
= descsize
+ bufferlen
;
809 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
811 /* Setup the descriptor */
812 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
813 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
814 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes granularity */
815 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
816 desc
.transactionid
= requestid
;
818 desc
.rangecount
= pagecount
;
820 for (i
= 0; i
< pagecount
; i
++) {
821 desc
.range
[i
].len
= pagebuffers
[i
].len
;
822 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
823 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
826 bufferlist
[0].iov_base
= &desc
;
827 bufferlist
[0].iov_len
= descsize
;
828 bufferlist
[1].iov_base
= buffer
;
829 bufferlist
[1].iov_len
= bufferlen
;
830 bufferlist
[2].iov_base
= &aligned_data
;
831 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
833 return hv_ringbuffer_write(channel
, bufferlist
, 3);
835 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
838 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
839 * using a GPADL Direct packet type.
840 * The buffer includes the vmbus descriptor.
842 int vmbus_sendpacket_mpb_desc(struct vmbus_channel
*channel
,
843 struct vmbus_packet_mpb_array
*desc
,
845 void *buffer
, u32 bufferlen
, u64 requestid
)
848 u32 packetlen_aligned
;
849 struct kvec bufferlist
[3];
850 u64 aligned_data
= 0;
852 packetlen
= desc_size
+ bufferlen
;
853 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
855 /* Setup the descriptor */
856 desc
->type
= VM_PKT_DATA_USING_GPA_DIRECT
;
857 desc
->flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
858 desc
->dataoffset8
= desc_size
>> 3; /* in 8-bytes granularity */
859 desc
->length8
= (u16
)(packetlen_aligned
>> 3);
860 desc
->transactionid
= requestid
;
862 desc
->rangecount
= 1;
864 bufferlist
[0].iov_base
= desc
;
865 bufferlist
[0].iov_len
= desc_size
;
866 bufferlist
[1].iov_base
= buffer
;
867 bufferlist
[1].iov_len
= bufferlen
;
868 bufferlist
[2].iov_base
= &aligned_data
;
869 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
871 return hv_ringbuffer_write(channel
, bufferlist
, 3);
873 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc
);
876 * __vmbus_recvpacket() - Retrieve the user packet on the specified channel
877 * @channel: Pointer to vmbus_channel structure
878 * @buffer: Pointer to the buffer you want to receive the data into.
879 * @bufferlen: Maximum size of what the buffer can hold.
880 * @buffer_actual_len: The actual size of the data after it was received.
881 * @requestid: Identifier of the request
882 * @raw: true means keep the vmpacket_descriptor header in the received data.
884 * Receives directly from the hyper-v vmbus and puts the data it received
885 * into Buffer. This will receive the data unparsed from hyper-v.
887 * Mainly used by Hyper-V drivers.
890 __vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
891 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
,
894 return hv_ringbuffer_read(channel
, buffer
, bufferlen
,
895 buffer_actual_len
, requestid
, raw
);
899 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
900 u32 bufferlen
, u32
*buffer_actual_len
,
903 return __vmbus_recvpacket(channel
, buffer
, bufferlen
,
904 buffer_actual_len
, requestid
, false);
906 EXPORT_SYMBOL(vmbus_recvpacket
);
909 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
911 int vmbus_recvpacket_raw(struct vmbus_channel
*channel
, void *buffer
,
912 u32 bufferlen
, u32
*buffer_actual_len
,
915 return __vmbus_recvpacket(channel
, buffer
, bufferlen
,
916 buffer_actual_len
, requestid
, true);
918 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);