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_get_debug_info -Retrieve various channel debug info
65 void vmbus_get_debug_info(struct vmbus_channel
*channel
,
66 struct vmbus_channel_debug_info
*debuginfo
)
68 struct hv_monitor_page
*monitorpage
;
69 u8 monitor_group
= (u8
)channel
->offermsg
.monitorid
/ 32;
70 u8 monitor_offset
= (u8
)channel
->offermsg
.monitorid
% 32;
72 monitorpage
= vmbus_connection
.monitor_pages
[0];
73 debuginfo
->servermonitor_connectionid
=
74 monitorpage
->parameter
[monitor_group
]
75 [monitor_offset
].connectionid
.u
.id
;
77 monitorpage
= vmbus_connection
.monitor_pages
[1];
78 debuginfo
->clientmonitor_connectionid
=
79 monitorpage
->parameter
[monitor_group
]
80 [monitor_offset
].connectionid
.u
.id
;
82 hv_ringbuffer_get_debuginfo(&channel
->inbound
, &debuginfo
->inbound
);
83 hv_ringbuffer_get_debuginfo(&channel
->outbound
, &debuginfo
->outbound
);
87 * vmbus_open - Open the specified channel.
89 int vmbus_open(struct vmbus_channel
*newchannel
, u32 send_ringbuffer_size
,
90 u32 recv_ringbuffer_size
, void *userdata
, u32 userdatalen
,
91 void (*onchannelcallback
)(void *context
), void *context
)
93 struct vmbus_channel_open_channel
*open_msg
;
94 struct vmbus_channel_msginfo
*open_info
= NULL
;
99 spin_lock_irqsave(&newchannel
->sc_lock
, flags
);
100 if (newchannel
->state
== CHANNEL_OPEN_STATE
) {
101 newchannel
->state
= CHANNEL_OPENING_STATE
;
103 spin_unlock_irqrestore(&newchannel
->sc_lock
, flags
);
106 spin_unlock_irqrestore(&newchannel
->sc_lock
, flags
);
108 newchannel
->onchannel_callback
= onchannelcallback
;
109 newchannel
->channel_callback_context
= context
;
111 /* Allocate the ring buffer */
112 out
= (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
113 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
119 in
= (void *)((unsigned long)out
+ send_ringbuffer_size
);
121 newchannel
->ringbuffer_pages
= out
;
122 newchannel
->ringbuffer_pagecount
= (send_ringbuffer_size
+
123 recv_ringbuffer_size
) >> PAGE_SHIFT
;
125 ret
= hv_ringbuffer_init(
126 &newchannel
->outbound
, out
, send_ringbuffer_size
);
133 ret
= hv_ringbuffer_init(
134 &newchannel
->inbound
, in
, recv_ringbuffer_size
);
141 /* Establish the gpadl for the ring buffer */
142 newchannel
->ringbuffer_gpadlhandle
= 0;
144 ret
= vmbus_establish_gpadl(newchannel
,
145 newchannel
->outbound
.ring_buffer
,
146 send_ringbuffer_size
+
147 recv_ringbuffer_size
,
148 &newchannel
->ringbuffer_gpadlhandle
);
155 /* Create and init the channel open message */
156 open_info
= kmalloc(sizeof(*open_info
) +
157 sizeof(struct vmbus_channel_open_channel
),
164 init_completion(&open_info
->waitevent
);
166 open_msg
= (struct vmbus_channel_open_channel
*)open_info
->msg
;
167 open_msg
->header
.msgtype
= CHANNELMSG_OPENCHANNEL
;
168 open_msg
->openid
= newchannel
->offermsg
.child_relid
;
169 open_msg
->child_relid
= newchannel
->offermsg
.child_relid
;
170 open_msg
->ringbuffer_gpadlhandle
= newchannel
->ringbuffer_gpadlhandle
;
171 open_msg
->downstream_ringbuffer_pageoffset
= send_ringbuffer_size
>>
173 open_msg
->target_vp
= newchannel
->target_vp
;
175 if (userdatalen
> MAX_USER_DEFINED_BYTES
) {
181 memcpy(open_msg
->userdata
, userdata
, userdatalen
);
183 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
184 list_add_tail(&open_info
->msglistentry
,
185 &vmbus_connection
.chn_msg_list
);
186 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
188 ret
= vmbus_post_msg(open_msg
,
189 sizeof(struct vmbus_channel_open_channel
));
194 t
= wait_for_completion_timeout(&open_info
->waitevent
, 5*HZ
);
201 if (open_info
->response
.open_result
.status
)
202 err
= open_info
->response
.open_result
.status
;
204 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
205 list_del(&open_info
->msglistentry
);
206 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
209 newchannel
->state
= CHANNEL_OPENED_STATE
;
215 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
216 list_del(&open_info
->msglistentry
);
217 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
220 free_pages((unsigned long)out
,
221 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
225 EXPORT_SYMBOL_GPL(vmbus_open
);
228 * create_gpadl_header - Creates a gpadl for the specified buffer
230 static int create_gpadl_header(void *kbuffer
, u32 size
,
231 struct vmbus_channel_msginfo
**msginfo
,
236 unsigned long long pfn
;
237 struct vmbus_channel_gpadl_header
*gpadl_header
;
238 struct vmbus_channel_gpadl_body
*gpadl_body
;
239 struct vmbus_channel_msginfo
*msgheader
;
240 struct vmbus_channel_msginfo
*msgbody
= NULL
;
243 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
245 pagecount
= size
>> PAGE_SHIFT
;
246 pfn
= virt_to_phys(kbuffer
) >> PAGE_SHIFT
;
248 /* do we need a gpadl body msg */
249 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
250 sizeof(struct vmbus_channel_gpadl_header
) -
251 sizeof(struct gpa_range
);
252 pfncount
= pfnsize
/ sizeof(u64
);
254 if (pagecount
> pfncount
) {
255 /* we need a gpadl body */
256 /* fill in the header */
257 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
258 sizeof(struct vmbus_channel_gpadl_header
) +
259 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
260 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
264 INIT_LIST_HEAD(&msgheader
->submsglist
);
265 msgheader
->msgsize
= msgsize
;
267 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
269 gpadl_header
->rangecount
= 1;
270 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
271 pagecount
* sizeof(u64
);
272 gpadl_header
->range
[0].byte_offset
= 0;
273 gpadl_header
->range
[0].byte_count
= size
;
274 for (i
= 0; i
< pfncount
; i
++)
275 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
276 *msginfo
= msgheader
;
280 pfnleft
= pagecount
- pfncount
;
282 /* how many pfns can we fit */
283 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
284 sizeof(struct vmbus_channel_gpadl_body
);
285 pfncount
= pfnsize
/ sizeof(u64
);
287 /* fill in the body */
289 if (pfnleft
> pfncount
)
294 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
295 sizeof(struct vmbus_channel_gpadl_body
) +
296 pfncurr
* sizeof(u64
);
297 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
300 struct vmbus_channel_msginfo
*pos
= NULL
;
301 struct vmbus_channel_msginfo
*tmp
= NULL
;
303 * Free up all the allocated messages.
305 list_for_each_entry_safe(pos
, tmp
,
306 &msgheader
->submsglist
,
309 list_del(&pos
->msglistentry
);
316 msgbody
->msgsize
= msgsize
;
319 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
322 * Gpadl is u32 and we are using a pointer which could
324 * This is governed by the guest/host protocol and
325 * so the hypervisor gurantees that this is ok.
327 for (i
= 0; i
< pfncurr
; i
++)
328 gpadl_body
->pfn
[i
] = pfn
+ pfnsum
+ i
;
330 /* add to msg header */
331 list_add_tail(&msgbody
->msglistentry
,
332 &msgheader
->submsglist
);
337 /* everything fits in a header */
338 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
339 sizeof(struct vmbus_channel_gpadl_header
) +
340 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
341 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
342 if (msgheader
== NULL
)
344 msgheader
->msgsize
= msgsize
;
346 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
348 gpadl_header
->rangecount
= 1;
349 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
350 pagecount
* sizeof(u64
);
351 gpadl_header
->range
[0].byte_offset
= 0;
352 gpadl_header
->range
[0].byte_count
= size
;
353 for (i
= 0; i
< pagecount
; i
++)
354 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
356 *msginfo
= msgheader
;
368 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
370 * @channel: a channel
371 * @kbuffer: from kmalloc
372 * @size: page-size multiple
373 * @gpadl_handle: some funky thing
375 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
376 u32 size
, u32
*gpadl_handle
)
378 struct vmbus_channel_gpadl_header
*gpadlmsg
;
379 struct vmbus_channel_gpadl_body
*gpadl_body
;
380 struct vmbus_channel_msginfo
*msginfo
= NULL
;
381 struct vmbus_channel_msginfo
*submsginfo
;
383 struct list_head
*curr
;
384 u32 next_gpadl_handle
;
389 next_gpadl_handle
= atomic_read(&vmbus_connection
.next_gpadl_handle
);
390 atomic_inc(&vmbus_connection
.next_gpadl_handle
);
392 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
, &msgcount
);
396 init_completion(&msginfo
->waitevent
);
398 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
399 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
400 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
401 gpadlmsg
->gpadl
= next_gpadl_handle
;
404 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
405 list_add_tail(&msginfo
->msglistentry
,
406 &vmbus_connection
.chn_msg_list
);
408 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
410 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
416 list_for_each(curr
, &msginfo
->submsglist
) {
418 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
420 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
422 gpadl_body
->header
.msgtype
=
423 CHANNELMSG_GPADL_BODY
;
424 gpadl_body
->gpadl
= next_gpadl_handle
;
426 ret
= vmbus_post_msg(gpadl_body
,
427 submsginfo
->msgsize
-
428 sizeof(*submsginfo
));
434 t
= wait_for_completion_timeout(&msginfo
->waitevent
, 5*HZ
);
438 /* At this point, we received the gpadl created msg */
439 *gpadl_handle
= gpadlmsg
->gpadl
;
442 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
443 list_del(&msginfo
->msglistentry
);
444 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
449 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
452 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
454 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
456 struct vmbus_channel_gpadl_teardown
*msg
;
457 struct vmbus_channel_msginfo
*info
;
461 info
= kmalloc(sizeof(*info
) +
462 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
466 init_completion(&info
->waitevent
);
468 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
470 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
471 msg
->child_relid
= channel
->offermsg
.child_relid
;
472 msg
->gpadl
= gpadl_handle
;
474 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
475 list_add_tail(&info
->msglistentry
,
476 &vmbus_connection
.chn_msg_list
);
477 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
478 ret
= vmbus_post_msg(msg
,
479 sizeof(struct vmbus_channel_gpadl_teardown
));
482 t
= wait_for_completion_timeout(&info
->waitevent
, 5*HZ
);
485 /* Received a torndown response */
486 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
487 list_del(&info
->msglistentry
);
488 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
493 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
495 static void vmbus_close_internal(struct vmbus_channel
*channel
)
497 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 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
505 channel
->onchannel_callback
= NULL
;
506 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
508 /* Send a closing message */
510 msg
= &channel
->close_msg
.msg
;
512 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
513 msg
->child_relid
= channel
->offermsg
.child_relid
;
515 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
));
518 /* Tear down the gpadl for the channel's ring buffer */
519 if (channel
->ringbuffer_gpadlhandle
)
520 vmbus_teardown_gpadl(channel
,
521 channel
->ringbuffer_gpadlhandle
);
523 /* Cleanup the ring buffers for this channel */
524 hv_ringbuffer_cleanup(&channel
->outbound
);
525 hv_ringbuffer_cleanup(&channel
->inbound
);
527 free_pages((unsigned long)channel
->ringbuffer_pages
,
528 get_order(channel
->ringbuffer_pagecount
* PAGE_SIZE
));
534 * vmbus_close - Close the specified channel
536 void vmbus_close(struct vmbus_channel
*channel
)
538 struct list_head
*cur
, *tmp
;
539 struct vmbus_channel
*cur_channel
;
541 if (channel
->primary_channel
!= NULL
) {
543 * We will only close sub-channels when
544 * the primary is closed.
549 * Close all the sub-channels first and then close the
552 list_for_each_safe(cur
, tmp
, &channel
->sc_list
) {
553 cur_channel
= list_entry(cur
, struct vmbus_channel
, sc_list
);
554 if (cur_channel
->state
!= CHANNEL_OPENED_STATE
)
556 vmbus_close_internal(cur_channel
);
559 * Now close the primary.
561 vmbus_close_internal(channel
);
563 EXPORT_SYMBOL_GPL(vmbus_close
);
566 * vmbus_sendpacket() - Send the specified buffer on the given channel
567 * @channel: Pointer to vmbus_channel structure.
568 * @buffer: Pointer to the buffer you want to receive the data into.
569 * @bufferlen: Maximum size of what the the buffer will hold
570 * @requestid: Identifier of the request
571 * @type: Type of packet that is being send e.g. negotiate, time
574 * Sends data in @buffer directly to hyper-v via the vmbus
575 * This will send the data unparsed to hyper-v.
577 * Mainly used by Hyper-V drivers.
579 int vmbus_sendpacket(struct vmbus_channel
*channel
, const void *buffer
,
580 u32 bufferlen
, u64 requestid
,
581 enum vmbus_packet_type type
, u32 flags
)
583 struct vmpacket_descriptor desc
;
584 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
585 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
586 struct scatterlist bufferlist
[3];
587 u64 aligned_data
= 0;
592 /* Setup the descriptor */
593 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
594 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
595 /* in 8-bytes granularity */
596 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
597 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
598 desc
.trans_id
= requestid
;
600 sg_init_table(bufferlist
, 3);
601 sg_set_buf(&bufferlist
[0], &desc
, sizeof(struct vmpacket_descriptor
));
602 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
603 sg_set_buf(&bufferlist
[2], &aligned_data
,
604 packetlen_aligned
- packetlen
);
606 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
608 if (ret
== 0 && signal
)
609 vmbus_setevent(channel
);
613 EXPORT_SYMBOL(vmbus_sendpacket
);
616 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
617 * packets using a GPADL Direct packet type.
619 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
620 struct hv_page_buffer pagebuffers
[],
621 u32 pagecount
, void *buffer
, u32 bufferlen
,
626 struct vmbus_channel_packet_page_buffer desc
;
629 u32 packetlen_aligned
;
630 struct scatterlist bufferlist
[3];
631 u64 aligned_data
= 0;
634 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
639 * Adjust the size down since vmbus_channel_packet_page_buffer is the
640 * largest size we support
642 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
643 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
644 sizeof(struct hv_page_buffer
));
645 packetlen
= descsize
+ bufferlen
;
646 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
648 /* Setup the descriptor */
649 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
650 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
651 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
652 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
653 desc
.transactionid
= requestid
;
654 desc
.rangecount
= pagecount
;
656 for (i
= 0; i
< pagecount
; i
++) {
657 desc
.range
[i
].len
= pagebuffers
[i
].len
;
658 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
659 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
662 sg_init_table(bufferlist
, 3);
663 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
664 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
665 sg_set_buf(&bufferlist
[2], &aligned_data
,
666 packetlen_aligned
- packetlen
);
668 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
670 if (ret
== 0 && signal
)
671 vmbus_setevent(channel
);
675 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
678 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
679 * using a GPADL Direct packet type.
681 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
682 struct hv_multipage_buffer
*multi_pagebuffer
,
683 void *buffer
, u32 bufferlen
, u64 requestid
)
686 struct vmbus_channel_packet_multipage_buffer desc
;
689 u32 packetlen_aligned
;
690 struct scatterlist bufferlist
[3];
691 u64 aligned_data
= 0;
693 u32 pfncount
= NUM_PAGES_SPANNED(multi_pagebuffer
->offset
,
694 multi_pagebuffer
->len
);
697 if ((pfncount
< 0) || (pfncount
> MAX_MULTIPAGE_BUFFER_COUNT
))
701 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
702 * the largest size we support
704 descsize
= sizeof(struct vmbus_channel_packet_multipage_buffer
) -
705 ((MAX_MULTIPAGE_BUFFER_COUNT
- pfncount
) *
707 packetlen
= descsize
+ bufferlen
;
708 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
711 /* Setup the descriptor */
712 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
713 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
714 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
715 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
716 desc
.transactionid
= requestid
;
719 desc
.range
.len
= multi_pagebuffer
->len
;
720 desc
.range
.offset
= multi_pagebuffer
->offset
;
722 memcpy(desc
.range
.pfn_array
, multi_pagebuffer
->pfn_array
,
723 pfncount
* sizeof(u64
));
725 sg_init_table(bufferlist
, 3);
726 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
727 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
728 sg_set_buf(&bufferlist
[2], &aligned_data
,
729 packetlen_aligned
- packetlen
);
731 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
733 if (ret
== 0 && signal
)
734 vmbus_setevent(channel
);
738 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer
);
741 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
742 * @channel: Pointer to vmbus_channel structure.
743 * @buffer: Pointer to the buffer you want to receive the data into.
744 * @bufferlen: Maximum size of what the the buffer will hold
745 * @buffer_actual_len: The actual size of the data after it was received
746 * @requestid: Identifier of the request
748 * Receives directly from the hyper-v vmbus and puts the data it received
749 * into Buffer. This will receive the data unparsed from hyper-v.
751 * Mainly used by Hyper-V drivers.
753 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
754 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
)
756 struct vmpacket_descriptor desc
;
762 *buffer_actual_len
= 0;
766 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
767 sizeof(struct vmpacket_descriptor
));
771 packetlen
= desc
.len8
<< 3;
772 userlen
= packetlen
- (desc
.offset8
<< 3);
774 *buffer_actual_len
= userlen
;
776 if (userlen
> bufferlen
) {
778 pr_err("Buffer too small - got %d needs %d\n",
783 *requestid
= desc
.trans_id
;
785 /* Copy over the packet to the user buffer */
786 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, userlen
,
787 (desc
.offset8
<< 3), &signal
);
790 vmbus_setevent(channel
);
794 EXPORT_SYMBOL(vmbus_recvpacket
);
797 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
799 int vmbus_recvpacket_raw(struct vmbus_channel
*channel
, void *buffer
,
800 u32 bufferlen
, u32
*buffer_actual_len
,
803 struct vmpacket_descriptor desc
;
808 *buffer_actual_len
= 0;
812 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
813 sizeof(struct vmpacket_descriptor
));
818 packetlen
= desc
.len8
<< 3;
820 *buffer_actual_len
= packetlen
;
822 if (packetlen
> bufferlen
) {
823 pr_err("Buffer too small - needed %d bytes but "
824 "got space for only %d bytes\n",
825 packetlen
, bufferlen
);
829 *requestid
= desc
.trans_id
;
831 /* Copy over the entire packet to the user buffer */
832 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, packetlen
, 0,
836 vmbus_setevent(channel
);
840 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);