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>
32 #include "hyperv_vmbus.h"
34 #define NUM_PAGES_SPANNED(addr, len) \
35 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
38 * vmbus_setevent- Trigger an event notification on the specified
41 static void vmbus_setevent(struct vmbus_channel
*channel
)
43 struct hv_monitor_page
*monitorpage
;
45 if (channel
->offermsg
.monitor_allocated
) {
46 /* Each u32 represents 32 channels */
47 sync_set_bit(channel
->offermsg
.child_relid
& 31,
48 (unsigned long *) vmbus_connection
.send_int_page
+
49 (channel
->offermsg
.child_relid
>> 5));
51 /* Get the child to parent monitor page */
52 monitorpage
= vmbus_connection
.monitor_pages
[1];
54 sync_set_bit(channel
->monitor_bit
,
55 (unsigned long *)&monitorpage
->trigger_group
56 [channel
->monitor_grp
].pending
);
59 vmbus_set_event(channel
);
64 * vmbus_open - Open the specified channel.
66 int vmbus_open(struct vmbus_channel
*newchannel
, u32 send_ringbuffer_size
,
67 u32 recv_ringbuffer_size
, void *userdata
, u32 userdatalen
,
68 void (*onchannelcallback
)(void *context
), void *context
)
70 struct vmbus_channel_open_channel
*open_msg
;
71 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 t
= wait_for_completion_timeout(&open_info
->waitevent
, 5*HZ
);
191 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
192 list_del(&open_info
->msglistentry
);
193 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
195 if (open_info
->response
.open_result
.status
) {
200 newchannel
->state
= CHANNEL_OPENED_STATE
;
205 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
206 list_del(&open_info
->msglistentry
);
207 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
210 vmbus_teardown_gpadl(newchannel
, newchannel
->ringbuffer_gpadlhandle
);
213 free_pages((unsigned long)out
,
214 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
216 newchannel
->state
= CHANNEL_OPEN_STATE
;
219 EXPORT_SYMBOL_GPL(vmbus_open
);
222 * create_gpadl_header - Creates a gpadl for the specified buffer
224 static int create_gpadl_header(void *kbuffer
, u32 size
,
225 struct vmbus_channel_msginfo
**msginfo
,
230 struct vmbus_channel_gpadl_header
*gpadl_header
;
231 struct vmbus_channel_gpadl_body
*gpadl_body
;
232 struct vmbus_channel_msginfo
*msgheader
;
233 struct vmbus_channel_msginfo
*msgbody
= NULL
;
236 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
238 pagecount
= size
>> PAGE_SHIFT
;
240 /* do we need a gpadl body msg */
241 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
242 sizeof(struct vmbus_channel_gpadl_header
) -
243 sizeof(struct gpa_range
);
244 pfncount
= pfnsize
/ sizeof(u64
);
246 if (pagecount
> pfncount
) {
247 /* we need a gpadl body */
248 /* fill in the header */
249 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
250 sizeof(struct vmbus_channel_gpadl_header
) +
251 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
252 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
256 INIT_LIST_HEAD(&msgheader
->submsglist
);
257 msgheader
->msgsize
= msgsize
;
259 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
261 gpadl_header
->rangecount
= 1;
262 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
263 pagecount
* sizeof(u64
);
264 gpadl_header
->range
[0].byte_offset
= 0;
265 gpadl_header
->range
[0].byte_count
= size
;
266 for (i
= 0; i
< pfncount
; i
++)
267 gpadl_header
->range
[0].pfn_array
[i
] = slow_virt_to_phys(
268 kbuffer
+ PAGE_SIZE
* i
) >> PAGE_SHIFT
;
269 *msginfo
= msgheader
;
273 pfnleft
= pagecount
- pfncount
;
275 /* how many pfns can we fit */
276 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
277 sizeof(struct vmbus_channel_gpadl_body
);
278 pfncount
= pfnsize
/ sizeof(u64
);
280 /* fill in the body */
282 if (pfnleft
> pfncount
)
287 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
288 sizeof(struct vmbus_channel_gpadl_body
) +
289 pfncurr
* sizeof(u64
);
290 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
293 struct vmbus_channel_msginfo
*pos
= NULL
;
294 struct vmbus_channel_msginfo
*tmp
= NULL
;
296 * Free up all the allocated messages.
298 list_for_each_entry_safe(pos
, tmp
,
299 &msgheader
->submsglist
,
302 list_del(&pos
->msglistentry
);
309 msgbody
->msgsize
= msgsize
;
312 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
315 * Gpadl is u32 and we are using a pointer which could
317 * This is governed by the guest/host protocol and
318 * so the hypervisor gurantees that this is ok.
320 for (i
= 0; i
< pfncurr
; i
++)
321 gpadl_body
->pfn
[i
] = slow_virt_to_phys(
322 kbuffer
+ PAGE_SIZE
* (pfnsum
+ i
)) >>
325 /* add to msg header */
326 list_add_tail(&msgbody
->msglistentry
,
327 &msgheader
->submsglist
);
332 /* everything fits in a header */
333 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
334 sizeof(struct vmbus_channel_gpadl_header
) +
335 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
336 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
337 if (msgheader
== NULL
)
339 msgheader
->msgsize
= msgsize
;
341 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
343 gpadl_header
->rangecount
= 1;
344 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
345 pagecount
* sizeof(u64
);
346 gpadl_header
->range
[0].byte_offset
= 0;
347 gpadl_header
->range
[0].byte_count
= size
;
348 for (i
= 0; i
< pagecount
; i
++)
349 gpadl_header
->range
[0].pfn_array
[i
] = slow_virt_to_phys(
350 kbuffer
+ PAGE_SIZE
* i
) >> PAGE_SHIFT
;
352 *msginfo
= msgheader
;
364 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
366 * @channel: a channel
367 * @kbuffer: from kmalloc or vmalloc
368 * @size: page-size multiple
369 * @gpadl_handle: some funky thing
371 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
372 u32 size
, u32
*gpadl_handle
)
374 struct vmbus_channel_gpadl_header
*gpadlmsg
;
375 struct vmbus_channel_gpadl_body
*gpadl_body
;
376 struct vmbus_channel_msginfo
*msginfo
= NULL
;
377 struct vmbus_channel_msginfo
*submsginfo
;
379 struct list_head
*curr
;
380 u32 next_gpadl_handle
;
385 (atomic_inc_return(&vmbus_connection
.next_gpadl_handle
) - 1);
387 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
, &msgcount
);
391 init_completion(&msginfo
->waitevent
);
393 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
394 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
395 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
396 gpadlmsg
->gpadl
= next_gpadl_handle
;
399 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
400 list_add_tail(&msginfo
->msglistentry
,
401 &vmbus_connection
.chn_msg_list
);
403 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
405 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
411 list_for_each(curr
, &msginfo
->submsglist
) {
413 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
415 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
417 gpadl_body
->header
.msgtype
=
418 CHANNELMSG_GPADL_BODY
;
419 gpadl_body
->gpadl
= next_gpadl_handle
;
421 ret
= vmbus_post_msg(gpadl_body
,
422 submsginfo
->msgsize
-
423 sizeof(*submsginfo
));
429 wait_for_completion(&msginfo
->waitevent
);
431 /* At this point, we received the gpadl created msg */
432 *gpadl_handle
= gpadlmsg
->gpadl
;
435 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
436 list_del(&msginfo
->msglistentry
);
437 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
442 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
445 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
447 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
449 struct vmbus_channel_gpadl_teardown
*msg
;
450 struct vmbus_channel_msginfo
*info
;
454 info
= kmalloc(sizeof(*info
) +
455 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
459 init_completion(&info
->waitevent
);
461 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
463 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
464 msg
->child_relid
= channel
->offermsg
.child_relid
;
465 msg
->gpadl
= gpadl_handle
;
467 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
468 list_add_tail(&info
->msglistentry
,
469 &vmbus_connection
.chn_msg_list
);
470 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
471 ret
= vmbus_post_msg(msg
,
472 sizeof(struct vmbus_channel_gpadl_teardown
));
477 wait_for_completion(&info
->waitevent
);
480 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
481 list_del(&info
->msglistentry
);
482 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
487 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
489 static void reset_channel_cb(void *arg
)
491 struct vmbus_channel
*channel
= arg
;
493 channel
->onchannel_callback
= NULL
;
496 static int vmbus_close_internal(struct vmbus_channel
*channel
)
498 struct vmbus_channel_close_channel
*msg
;
501 channel
->state
= CHANNEL_OPEN_STATE
;
502 channel
->sc_creation_callback
= NULL
;
503 /* Stop callback and cancel the timer asap */
504 if (channel
->target_cpu
!= get_cpu()) {
506 smp_call_function_single(channel
->target_cpu
, reset_channel_cb
,
509 reset_channel_cb(channel
);
513 /* Send a closing message */
515 msg
= &channel
->close_msg
.msg
;
517 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
518 msg
->child_relid
= channel
->offermsg
.child_relid
;
520 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
));
523 pr_err("Close failed: close post msg return is %d\n", ret
);
525 * If we failed to post the close msg,
526 * it is perhaps better to leak memory.
531 /* Tear down the gpadl for the channel's ring buffer */
532 if (channel
->ringbuffer_gpadlhandle
) {
533 ret
= vmbus_teardown_gpadl(channel
,
534 channel
->ringbuffer_gpadlhandle
);
536 pr_err("Close failed: teardown gpadl return %d\n", ret
);
538 * If we failed to teardown gpadl,
539 * it is perhaps better to leak memory.
545 /* Cleanup the ring buffers for this channel */
546 hv_ringbuffer_cleanup(&channel
->outbound
);
547 hv_ringbuffer_cleanup(&channel
->inbound
);
549 free_pages((unsigned long)channel
->ringbuffer_pages
,
550 get_order(channel
->ringbuffer_pagecount
* PAGE_SIZE
));
553 * If the channel has been rescinded; process device removal.
555 if (channel
->rescind
)
556 hv_process_channel_removal(channel
,
557 channel
->offermsg
.child_relid
);
562 * vmbus_close - Close the specified channel
564 void vmbus_close(struct vmbus_channel
*channel
)
566 struct list_head
*cur
, *tmp
;
567 struct vmbus_channel
*cur_channel
;
569 if (channel
->primary_channel
!= NULL
) {
571 * We will only close sub-channels when
572 * the primary is closed.
577 * Close all the sub-channels first and then close the
580 list_for_each_safe(cur
, tmp
, &channel
->sc_list
) {
581 cur_channel
= list_entry(cur
, struct vmbus_channel
, sc_list
);
582 if (cur_channel
->state
!= CHANNEL_OPENED_STATE
)
584 vmbus_close_internal(cur_channel
);
587 * Now close the primary.
589 vmbus_close_internal(channel
);
591 EXPORT_SYMBOL_GPL(vmbus_close
);
593 int vmbus_sendpacket_ctl(struct vmbus_channel
*channel
, void *buffer
,
594 u32 bufferlen
, u64 requestid
,
595 enum vmbus_packet_type type
, u32 flags
, bool kick_q
)
597 struct vmpacket_descriptor desc
;
598 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
599 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
600 struct kvec bufferlist
[3];
601 u64 aligned_data
= 0;
604 int num_vecs
= ((bufferlen
!= 0) ? 3 : 1);
607 /* Setup the descriptor */
608 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
609 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
610 /* in 8-bytes granularity */
611 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
612 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
613 desc
.trans_id
= requestid
;
615 bufferlist
[0].iov_base
= &desc
;
616 bufferlist
[0].iov_len
= sizeof(struct vmpacket_descriptor
);
617 bufferlist
[1].iov_base
= buffer
;
618 bufferlist
[1].iov_len
= bufferlen
;
619 bufferlist
[2].iov_base
= &aligned_data
;
620 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
622 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, num_vecs
,
626 * Signalling the host is conditional on many factors:
627 * 1. The ring state changed from being empty to non-empty.
628 * This is tracked by the variable "signal".
629 * 2. The variable kick_q tracks if more data will be placed
630 * on the ring. We will not signal if more data is
633 * If we cannot write to the ring-buffer; signal the host
634 * even if we may not have written anything. This is a rare
635 * enough condition that it should not matter.
637 if (((ret
== 0) && kick_q
&& signal
) || (ret
))
638 vmbus_setevent(channel
);
642 EXPORT_SYMBOL(vmbus_sendpacket_ctl
);
645 * vmbus_sendpacket() - Send the specified buffer on the given channel
646 * @channel: Pointer to vmbus_channel structure.
647 * @buffer: Pointer to the buffer you want to receive the data into.
648 * @bufferlen: Maximum size of what the the buffer will hold
649 * @requestid: Identifier of the request
650 * @type: Type of packet that is being send e.g. negotiate, time
653 * Sends data in @buffer directly to hyper-v via the vmbus
654 * This will send the data unparsed to hyper-v.
656 * Mainly used by Hyper-V drivers.
658 int vmbus_sendpacket(struct vmbus_channel
*channel
, void *buffer
,
659 u32 bufferlen
, u64 requestid
,
660 enum vmbus_packet_type type
, u32 flags
)
662 return vmbus_sendpacket_ctl(channel
, buffer
, bufferlen
, requestid
,
665 EXPORT_SYMBOL(vmbus_sendpacket
);
668 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
669 * packets using a GPADL Direct packet type. This interface allows you
670 * to control notifying the host. This will be useful for sending
671 * batched data. Also the sender can control the send flags
674 int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel
*channel
,
675 struct hv_page_buffer pagebuffers
[],
676 u32 pagecount
, void *buffer
, u32 bufferlen
,
683 struct vmbus_channel_packet_page_buffer desc
;
686 u32 packetlen_aligned
;
687 struct kvec bufferlist
[3];
688 u64 aligned_data
= 0;
691 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
696 * Adjust the size down since vmbus_channel_packet_page_buffer is the
697 * largest size we support
699 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
700 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
701 sizeof(struct hv_page_buffer
));
702 packetlen
= descsize
+ bufferlen
;
703 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
705 /* Setup the descriptor */
706 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
708 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
709 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
710 desc
.transactionid
= requestid
;
711 desc
.rangecount
= pagecount
;
713 for (i
= 0; i
< pagecount
; i
++) {
714 desc
.range
[i
].len
= pagebuffers
[i
].len
;
715 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
716 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
719 bufferlist
[0].iov_base
= &desc
;
720 bufferlist
[0].iov_len
= descsize
;
721 bufferlist
[1].iov_base
= buffer
;
722 bufferlist
[1].iov_len
= bufferlen
;
723 bufferlist
[2].iov_base
= &aligned_data
;
724 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
726 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
729 * Signalling the host is conditional on many factors:
730 * 1. The ring state changed from being empty to non-empty.
731 * This is tracked by the variable "signal".
732 * 2. The variable kick_q tracks if more data will be placed
733 * on the ring. We will not signal if more data is
736 * If we cannot write to the ring-buffer; signal the host
737 * even if we may not have written anything. This is a rare
738 * enough condition that it should not matter.
740 if (((ret
== 0) && kick_q
&& signal
) || (ret
))
741 vmbus_setevent(channel
);
745 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl
);
748 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
749 * packets using a GPADL Direct packet type.
751 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
752 struct hv_page_buffer pagebuffers
[],
753 u32 pagecount
, void *buffer
, u32 bufferlen
,
756 u32 flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
757 return vmbus_sendpacket_pagebuffer_ctl(channel
, pagebuffers
, pagecount
,
758 buffer
, bufferlen
, requestid
,
762 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
765 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
766 * using a GPADL Direct packet type.
767 * The buffer includes the vmbus descriptor.
769 int vmbus_sendpacket_mpb_desc(struct vmbus_channel
*channel
,
770 struct vmbus_packet_mpb_array
*desc
,
772 void *buffer
, u32 bufferlen
, u64 requestid
)
776 u32 packetlen_aligned
;
777 struct kvec bufferlist
[3];
778 u64 aligned_data
= 0;
781 packetlen
= desc_size
+ bufferlen
;
782 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
784 /* Setup the descriptor */
785 desc
->type
= VM_PKT_DATA_USING_GPA_DIRECT
;
786 desc
->flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
787 desc
->dataoffset8
= desc_size
>> 3; /* in 8-bytes grandularity */
788 desc
->length8
= (u16
)(packetlen_aligned
>> 3);
789 desc
->transactionid
= requestid
;
790 desc
->rangecount
= 1;
792 bufferlist
[0].iov_base
= desc
;
793 bufferlist
[0].iov_len
= desc_size
;
794 bufferlist
[1].iov_base
= buffer
;
795 bufferlist
[1].iov_len
= bufferlen
;
796 bufferlist
[2].iov_base
= &aligned_data
;
797 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
799 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
801 if (ret
== 0 && signal
)
802 vmbus_setevent(channel
);
806 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc
);
809 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
810 * using a GPADL Direct packet type.
812 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
813 struct hv_multipage_buffer
*multi_pagebuffer
,
814 void *buffer
, u32 bufferlen
, u64 requestid
)
817 struct vmbus_channel_packet_multipage_buffer desc
;
820 u32 packetlen_aligned
;
821 struct kvec bufferlist
[3];
822 u64 aligned_data
= 0;
824 u32 pfncount
= NUM_PAGES_SPANNED(multi_pagebuffer
->offset
,
825 multi_pagebuffer
->len
);
827 if (pfncount
> MAX_MULTIPAGE_BUFFER_COUNT
)
831 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
832 * the largest size we support
834 descsize
= sizeof(struct vmbus_channel_packet_multipage_buffer
) -
835 ((MAX_MULTIPAGE_BUFFER_COUNT
- pfncount
) *
837 packetlen
= descsize
+ bufferlen
;
838 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
841 /* Setup the descriptor */
842 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
843 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
844 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
845 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
846 desc
.transactionid
= requestid
;
849 desc
.range
.len
= multi_pagebuffer
->len
;
850 desc
.range
.offset
= multi_pagebuffer
->offset
;
852 memcpy(desc
.range
.pfn_array
, multi_pagebuffer
->pfn_array
,
853 pfncount
* sizeof(u64
));
855 bufferlist
[0].iov_base
= &desc
;
856 bufferlist
[0].iov_len
= descsize
;
857 bufferlist
[1].iov_base
= buffer
;
858 bufferlist
[1].iov_len
= bufferlen
;
859 bufferlist
[2].iov_base
= &aligned_data
;
860 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
862 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
864 if (ret
== 0 && signal
)
865 vmbus_setevent(channel
);
869 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer
);
872 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
873 * @channel: Pointer to vmbus_channel structure.
874 * @buffer: Pointer to the buffer you want to receive the data into.
875 * @bufferlen: Maximum size of what the the buffer will hold
876 * @buffer_actual_len: The actual size of the data after it was received
877 * @requestid: Identifier of the request
879 * Receives directly from the hyper-v vmbus and puts the data it received
880 * into Buffer. This will receive the data unparsed from hyper-v.
882 * Mainly used by Hyper-V drivers.
884 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
885 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
)
887 struct vmpacket_descriptor desc
;
893 *buffer_actual_len
= 0;
897 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
898 sizeof(struct vmpacket_descriptor
));
902 packetlen
= desc
.len8
<< 3;
903 userlen
= packetlen
- (desc
.offset8
<< 3);
905 *buffer_actual_len
= userlen
;
907 if (userlen
> bufferlen
) {
909 pr_err("Buffer too small - got %d needs %d\n",
914 *requestid
= desc
.trans_id
;
916 /* Copy over the packet to the user buffer */
917 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, userlen
,
918 (desc
.offset8
<< 3), &signal
);
921 vmbus_setevent(channel
);
925 EXPORT_SYMBOL(vmbus_recvpacket
);
928 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
930 int vmbus_recvpacket_raw(struct vmbus_channel
*channel
, void *buffer
,
931 u32 bufferlen
, u32
*buffer_actual_len
,
934 struct vmpacket_descriptor desc
;
939 *buffer_actual_len
= 0;
943 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
944 sizeof(struct vmpacket_descriptor
));
949 packetlen
= desc
.len8
<< 3;
951 *buffer_actual_len
= packetlen
;
953 if (packetlen
> bufferlen
)
956 *requestid
= desc
.trans_id
;
958 /* Copy over the entire packet to the user buffer */
959 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, packetlen
, 0,
963 vmbus_setevent(channel
);
967 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);