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>
31 #include "hyperv_vmbus.h"
33 #define NUM_PAGES_SPANNED(addr, len) \
34 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
37 * vmbus_setevent- Trigger an event notification on the specified
40 static void vmbus_setevent(struct vmbus_channel
*channel
)
42 struct hv_monitor_page
*monitorpage
;
44 if (channel
->offermsg
.monitor_allocated
) {
45 /* Each u32 represents 32 channels */
46 sync_set_bit(channel
->offermsg
.child_relid
& 31,
47 (unsigned long *) vmbus_connection
.send_int_page
+
48 (channel
->offermsg
.child_relid
>> 5));
50 /* Get the child to parent monitor page */
51 monitorpage
= vmbus_connection
.monitor_pages
[1];
53 sync_set_bit(channel
->monitor_bit
,
54 (unsigned long *)&monitorpage
->trigger_group
55 [channel
->monitor_grp
].pending
);
58 vmbus_set_event(channel
);
63 * vmbus_open - Open the specified channel.
65 int vmbus_open(struct vmbus_channel
*newchannel
, u32 send_ringbuffer_size
,
66 u32 recv_ringbuffer_size
, void *userdata
, u32 userdatalen
,
67 void (*onchannelcallback
)(void *context
), void *context
)
69 struct vmbus_channel_open_channel
*open_msg
;
70 struct vmbus_channel_msginfo
*open_info
= NULL
;
75 spin_lock_irqsave(&newchannel
->sc_lock
, flags
);
76 if (newchannel
->state
== CHANNEL_OPEN_STATE
) {
77 newchannel
->state
= CHANNEL_OPENING_STATE
;
79 spin_unlock_irqrestore(&newchannel
->sc_lock
, flags
);
82 spin_unlock_irqrestore(&newchannel
->sc_lock
, flags
);
84 newchannel
->onchannel_callback
= onchannelcallback
;
85 newchannel
->channel_callback_context
= context
;
87 /* Allocate the ring buffer */
88 out
= (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
89 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
95 in
= (void *)((unsigned long)out
+ send_ringbuffer_size
);
97 newchannel
->ringbuffer_pages
= out
;
98 newchannel
->ringbuffer_pagecount
= (send_ringbuffer_size
+
99 recv_ringbuffer_size
) >> PAGE_SHIFT
;
101 ret
= hv_ringbuffer_init(
102 &newchannel
->outbound
, out
, send_ringbuffer_size
);
109 ret
= hv_ringbuffer_init(
110 &newchannel
->inbound
, in
, recv_ringbuffer_size
);
117 /* Establish the gpadl for the ring buffer */
118 newchannel
->ringbuffer_gpadlhandle
= 0;
120 ret
= vmbus_establish_gpadl(newchannel
,
121 newchannel
->outbound
.ring_buffer
,
122 send_ringbuffer_size
+
123 recv_ringbuffer_size
,
124 &newchannel
->ringbuffer_gpadlhandle
);
131 /* Create and init the channel open message */
132 open_info
= kmalloc(sizeof(*open_info
) +
133 sizeof(struct vmbus_channel_open_channel
),
140 init_completion(&open_info
->waitevent
);
142 open_msg
= (struct vmbus_channel_open_channel
*)open_info
->msg
;
143 open_msg
->header
.msgtype
= CHANNELMSG_OPENCHANNEL
;
144 open_msg
->openid
= newchannel
->offermsg
.child_relid
;
145 open_msg
->child_relid
= newchannel
->offermsg
.child_relid
;
146 open_msg
->ringbuffer_gpadlhandle
= newchannel
->ringbuffer_gpadlhandle
;
147 open_msg
->downstream_ringbuffer_pageoffset
= send_ringbuffer_size
>>
149 open_msg
->target_vp
= newchannel
->target_vp
;
151 if (userdatalen
> MAX_USER_DEFINED_BYTES
) {
157 memcpy(open_msg
->userdata
, userdata
, userdatalen
);
159 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
160 list_add_tail(&open_info
->msglistentry
,
161 &vmbus_connection
.chn_msg_list
);
162 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
164 ret
= vmbus_post_msg(open_msg
,
165 sizeof(struct vmbus_channel_open_channel
));
170 t
= wait_for_completion_timeout(&open_info
->waitevent
, 5*HZ
);
177 if (open_info
->response
.open_result
.status
)
178 err
= open_info
->response
.open_result
.status
;
180 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
181 list_del(&open_info
->msglistentry
);
182 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
185 newchannel
->state
= CHANNEL_OPENED_STATE
;
191 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
192 list_del(&open_info
->msglistentry
);
193 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
196 free_pages((unsigned long)out
,
197 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
201 EXPORT_SYMBOL_GPL(vmbus_open
);
204 * create_gpadl_header - Creates a gpadl for the specified buffer
206 static int create_gpadl_header(void *kbuffer
, u32 size
,
207 struct vmbus_channel_msginfo
**msginfo
,
212 unsigned long long pfn
;
213 struct vmbus_channel_gpadl_header
*gpadl_header
;
214 struct vmbus_channel_gpadl_body
*gpadl_body
;
215 struct vmbus_channel_msginfo
*msgheader
;
216 struct vmbus_channel_msginfo
*msgbody
= NULL
;
219 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
221 pagecount
= size
>> PAGE_SHIFT
;
222 pfn
= virt_to_phys(kbuffer
) >> PAGE_SHIFT
;
224 /* do we need a gpadl body msg */
225 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
226 sizeof(struct vmbus_channel_gpadl_header
) -
227 sizeof(struct gpa_range
);
228 pfncount
= pfnsize
/ sizeof(u64
);
230 if (pagecount
> pfncount
) {
231 /* we need a gpadl body */
232 /* fill in the header */
233 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
234 sizeof(struct vmbus_channel_gpadl_header
) +
235 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
236 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
240 INIT_LIST_HEAD(&msgheader
->submsglist
);
241 msgheader
->msgsize
= msgsize
;
243 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
245 gpadl_header
->rangecount
= 1;
246 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
247 pagecount
* sizeof(u64
);
248 gpadl_header
->range
[0].byte_offset
= 0;
249 gpadl_header
->range
[0].byte_count
= size
;
250 for (i
= 0; i
< pfncount
; i
++)
251 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
252 *msginfo
= msgheader
;
256 pfnleft
= pagecount
- pfncount
;
258 /* how many pfns can we fit */
259 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
260 sizeof(struct vmbus_channel_gpadl_body
);
261 pfncount
= pfnsize
/ sizeof(u64
);
263 /* fill in the body */
265 if (pfnleft
> pfncount
)
270 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
271 sizeof(struct vmbus_channel_gpadl_body
) +
272 pfncurr
* sizeof(u64
);
273 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
276 struct vmbus_channel_msginfo
*pos
= NULL
;
277 struct vmbus_channel_msginfo
*tmp
= NULL
;
279 * Free up all the allocated messages.
281 list_for_each_entry_safe(pos
, tmp
,
282 &msgheader
->submsglist
,
285 list_del(&pos
->msglistentry
);
292 msgbody
->msgsize
= msgsize
;
295 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
298 * Gpadl is u32 and we are using a pointer which could
300 * This is governed by the guest/host protocol and
301 * so the hypervisor gurantees that this is ok.
303 for (i
= 0; i
< pfncurr
; i
++)
304 gpadl_body
->pfn
[i
] = pfn
+ pfnsum
+ i
;
306 /* add to msg header */
307 list_add_tail(&msgbody
->msglistentry
,
308 &msgheader
->submsglist
);
313 /* everything fits in a header */
314 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
315 sizeof(struct vmbus_channel_gpadl_header
) +
316 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
317 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
318 if (msgheader
== NULL
)
320 msgheader
->msgsize
= msgsize
;
322 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
324 gpadl_header
->rangecount
= 1;
325 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
326 pagecount
* sizeof(u64
);
327 gpadl_header
->range
[0].byte_offset
= 0;
328 gpadl_header
->range
[0].byte_count
= size
;
329 for (i
= 0; i
< pagecount
; i
++)
330 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
332 *msginfo
= msgheader
;
344 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
346 * @channel: a channel
347 * @kbuffer: from kmalloc
348 * @size: page-size multiple
349 * @gpadl_handle: some funky thing
351 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
352 u32 size
, u32
*gpadl_handle
)
354 struct vmbus_channel_gpadl_header
*gpadlmsg
;
355 struct vmbus_channel_gpadl_body
*gpadl_body
;
356 struct vmbus_channel_msginfo
*msginfo
= NULL
;
357 struct vmbus_channel_msginfo
*submsginfo
;
359 struct list_head
*curr
;
360 u32 next_gpadl_handle
;
365 next_gpadl_handle
= atomic_read(&vmbus_connection
.next_gpadl_handle
);
366 atomic_inc(&vmbus_connection
.next_gpadl_handle
);
368 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
, &msgcount
);
372 init_completion(&msginfo
->waitevent
);
374 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
375 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
376 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
377 gpadlmsg
->gpadl
= next_gpadl_handle
;
380 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
381 list_add_tail(&msginfo
->msglistentry
,
382 &vmbus_connection
.chn_msg_list
);
384 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
386 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
392 list_for_each(curr
, &msginfo
->submsglist
) {
394 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
396 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
398 gpadl_body
->header
.msgtype
=
399 CHANNELMSG_GPADL_BODY
;
400 gpadl_body
->gpadl
= next_gpadl_handle
;
402 ret
= vmbus_post_msg(gpadl_body
,
403 submsginfo
->msgsize
-
404 sizeof(*submsginfo
));
410 t
= wait_for_completion_timeout(&msginfo
->waitevent
, 5*HZ
);
414 /* At this point, we received the gpadl created msg */
415 *gpadl_handle
= gpadlmsg
->gpadl
;
418 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
419 list_del(&msginfo
->msglistentry
);
420 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
425 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
428 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
430 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
432 struct vmbus_channel_gpadl_teardown
*msg
;
433 struct vmbus_channel_msginfo
*info
;
437 info
= kmalloc(sizeof(*info
) +
438 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
442 init_completion(&info
->waitevent
);
444 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
446 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
447 msg
->child_relid
= channel
->offermsg
.child_relid
;
448 msg
->gpadl
= gpadl_handle
;
450 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
451 list_add_tail(&info
->msglistentry
,
452 &vmbus_connection
.chn_msg_list
);
453 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
454 ret
= vmbus_post_msg(msg
,
455 sizeof(struct vmbus_channel_gpadl_teardown
));
458 t
= wait_for_completion_timeout(&info
->waitevent
, 5*HZ
);
461 /* Received a torndown response */
462 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
463 list_del(&info
->msglistentry
);
464 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
469 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
471 static void vmbus_close_internal(struct vmbus_channel
*channel
)
473 struct vmbus_channel_close_channel
*msg
;
477 channel
->state
= CHANNEL_OPEN_STATE
;
478 channel
->sc_creation_callback
= NULL
;
479 /* Stop callback and cancel the timer asap */
480 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
481 channel
->onchannel_callback
= NULL
;
482 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
484 /* Send a closing message */
486 msg
= &channel
->close_msg
.msg
;
488 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
489 msg
->child_relid
= channel
->offermsg
.child_relid
;
491 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
));
494 /* Tear down the gpadl for the channel's ring buffer */
495 if (channel
->ringbuffer_gpadlhandle
)
496 vmbus_teardown_gpadl(channel
,
497 channel
->ringbuffer_gpadlhandle
);
499 /* Cleanup the ring buffers for this channel */
500 hv_ringbuffer_cleanup(&channel
->outbound
);
501 hv_ringbuffer_cleanup(&channel
->inbound
);
503 free_pages((unsigned long)channel
->ringbuffer_pages
,
504 get_order(channel
->ringbuffer_pagecount
* PAGE_SIZE
));
510 * vmbus_close - Close the specified channel
512 void vmbus_close(struct vmbus_channel
*channel
)
514 struct list_head
*cur
, *tmp
;
515 struct vmbus_channel
*cur_channel
;
517 if (channel
->primary_channel
!= NULL
) {
519 * We will only close sub-channels when
520 * the primary is closed.
525 * Close all the sub-channels first and then close the
528 list_for_each_safe(cur
, tmp
, &channel
->sc_list
) {
529 cur_channel
= list_entry(cur
, struct vmbus_channel
, sc_list
);
530 if (cur_channel
->state
!= CHANNEL_OPENED_STATE
)
532 vmbus_close_internal(cur_channel
);
535 * Now close the primary.
537 vmbus_close_internal(channel
);
539 EXPORT_SYMBOL_GPL(vmbus_close
);
542 * vmbus_sendpacket() - Send the specified buffer on the given channel
543 * @channel: Pointer to vmbus_channel structure.
544 * @buffer: Pointer to the buffer you want to receive the data into.
545 * @bufferlen: Maximum size of what the the buffer will hold
546 * @requestid: Identifier of the request
547 * @type: Type of packet that is being send e.g. negotiate, time
550 * Sends data in @buffer directly to hyper-v via the vmbus
551 * This will send the data unparsed to hyper-v.
553 * Mainly used by Hyper-V drivers.
555 int vmbus_sendpacket(struct vmbus_channel
*channel
, const void *buffer
,
556 u32 bufferlen
, u64 requestid
,
557 enum vmbus_packet_type type
, u32 flags
)
559 struct vmpacket_descriptor desc
;
560 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
561 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
562 struct scatterlist bufferlist
[3];
563 u64 aligned_data
= 0;
568 /* Setup the descriptor */
569 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
570 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
571 /* in 8-bytes granularity */
572 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
573 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
574 desc
.trans_id
= requestid
;
576 sg_init_table(bufferlist
, 3);
577 sg_set_buf(&bufferlist
[0], &desc
, sizeof(struct vmpacket_descriptor
));
578 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
579 sg_set_buf(&bufferlist
[2], &aligned_data
,
580 packetlen_aligned
- packetlen
);
582 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
584 if (ret
== 0 && signal
)
585 vmbus_setevent(channel
);
589 EXPORT_SYMBOL(vmbus_sendpacket
);
592 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
593 * packets using a GPADL Direct packet type.
595 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
596 struct hv_page_buffer pagebuffers
[],
597 u32 pagecount
, void *buffer
, u32 bufferlen
,
602 struct vmbus_channel_packet_page_buffer desc
;
605 u32 packetlen_aligned
;
606 struct scatterlist bufferlist
[3];
607 u64 aligned_data
= 0;
610 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
615 * Adjust the size down since vmbus_channel_packet_page_buffer is the
616 * largest size we support
618 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
619 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
620 sizeof(struct hv_page_buffer
));
621 packetlen
= descsize
+ bufferlen
;
622 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
624 /* Setup the descriptor */
625 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
626 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
627 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
628 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
629 desc
.transactionid
= requestid
;
630 desc
.rangecount
= pagecount
;
632 for (i
= 0; i
< pagecount
; i
++) {
633 desc
.range
[i
].len
= pagebuffers
[i
].len
;
634 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
635 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
638 sg_init_table(bufferlist
, 3);
639 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
640 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
641 sg_set_buf(&bufferlist
[2], &aligned_data
,
642 packetlen_aligned
- packetlen
);
644 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
646 if (ret
== 0 && signal
)
647 vmbus_setevent(channel
);
651 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
654 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
655 * using a GPADL Direct packet type.
657 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
658 struct hv_multipage_buffer
*multi_pagebuffer
,
659 void *buffer
, u32 bufferlen
, u64 requestid
)
662 struct vmbus_channel_packet_multipage_buffer desc
;
665 u32 packetlen_aligned
;
666 struct scatterlist bufferlist
[3];
667 u64 aligned_data
= 0;
669 u32 pfncount
= NUM_PAGES_SPANNED(multi_pagebuffer
->offset
,
670 multi_pagebuffer
->len
);
673 if ((pfncount
< 0) || (pfncount
> MAX_MULTIPAGE_BUFFER_COUNT
))
677 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
678 * the largest size we support
680 descsize
= sizeof(struct vmbus_channel_packet_multipage_buffer
) -
681 ((MAX_MULTIPAGE_BUFFER_COUNT
- pfncount
) *
683 packetlen
= descsize
+ bufferlen
;
684 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
687 /* Setup the descriptor */
688 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
689 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
690 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
691 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
692 desc
.transactionid
= requestid
;
695 desc
.range
.len
= multi_pagebuffer
->len
;
696 desc
.range
.offset
= multi_pagebuffer
->offset
;
698 memcpy(desc
.range
.pfn_array
, multi_pagebuffer
->pfn_array
,
699 pfncount
* sizeof(u64
));
701 sg_init_table(bufferlist
, 3);
702 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
703 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
704 sg_set_buf(&bufferlist
[2], &aligned_data
,
705 packetlen_aligned
- packetlen
);
707 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
709 if (ret
== 0 && signal
)
710 vmbus_setevent(channel
);
714 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer
);
717 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
718 * @channel: Pointer to vmbus_channel structure.
719 * @buffer: Pointer to the buffer you want to receive the data into.
720 * @bufferlen: Maximum size of what the the buffer will hold
721 * @buffer_actual_len: The actual size of the data after it was received
722 * @requestid: Identifier of the request
724 * Receives directly from the hyper-v vmbus and puts the data it received
725 * into Buffer. This will receive the data unparsed from hyper-v.
727 * Mainly used by Hyper-V drivers.
729 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
730 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
)
732 struct vmpacket_descriptor desc
;
738 *buffer_actual_len
= 0;
742 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
743 sizeof(struct vmpacket_descriptor
));
747 packetlen
= desc
.len8
<< 3;
748 userlen
= packetlen
- (desc
.offset8
<< 3);
750 *buffer_actual_len
= userlen
;
752 if (userlen
> bufferlen
) {
754 pr_err("Buffer too small - got %d needs %d\n",
759 *requestid
= desc
.trans_id
;
761 /* Copy over the packet to the user buffer */
762 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, userlen
,
763 (desc
.offset8
<< 3), &signal
);
766 vmbus_setevent(channel
);
770 EXPORT_SYMBOL(vmbus_recvpacket
);
773 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
775 int vmbus_recvpacket_raw(struct vmbus_channel
*channel
, void *buffer
,
776 u32 bufferlen
, u32
*buffer_actual_len
,
779 struct vmpacket_descriptor desc
;
784 *buffer_actual_len
= 0;
788 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
789 sizeof(struct vmpacket_descriptor
));
794 packetlen
= desc
.len8
<< 3;
796 *buffer_actual_len
= packetlen
;
798 if (packetlen
> bufferlen
) {
799 pr_err("Buffer too small - needed %d bytes but "
800 "got space for only %d bytes\n",
801 packetlen
, bufferlen
);
805 *requestid
= desc
.trans_id
;
807 /* Copy over the entire packet to the user buffer */
808 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, packetlen
, 0,
812 vmbus_setevent(channel
);
816 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);