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 monitorpage
= vmbus_connection
.monitor_pages
;
51 monitorpage
++; /* Get the child to parent monitor page */
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 debuginfo
->relid
= channel
->offermsg
.child_relid
;
73 debuginfo
->state
= channel
->state
;
74 memcpy(&debuginfo
->interfacetype
,
75 &channel
->offermsg
.offer
.if_type
, sizeof(uuid_le
));
76 memcpy(&debuginfo
->interface_instance
,
77 &channel
->offermsg
.offer
.if_instance
,
80 monitorpage
= (struct hv_monitor_page
*)vmbus_connection
.monitor_pages
;
82 debuginfo
->monitorid
= channel
->offermsg
.monitorid
;
84 debuginfo
->servermonitor_pending
=
85 monitorpage
->trigger_group
[monitor_group
].pending
;
86 debuginfo
->servermonitor_latency
=
87 monitorpage
->latency
[monitor_group
][monitor_offset
];
88 debuginfo
->servermonitor_connectionid
=
89 monitorpage
->parameter
[monitor_group
]
90 [monitor_offset
].connectionid
.u
.id
;
94 debuginfo
->clientmonitor_pending
=
95 monitorpage
->trigger_group
[monitor_group
].pending
;
96 debuginfo
->clientmonitor_latency
=
97 monitorpage
->latency
[monitor_group
][monitor_offset
];
98 debuginfo
->clientmonitor_connectionid
=
99 monitorpage
->parameter
[monitor_group
]
100 [monitor_offset
].connectionid
.u
.id
;
102 hv_ringbuffer_get_debuginfo(&channel
->inbound
, &debuginfo
->inbound
);
103 hv_ringbuffer_get_debuginfo(&channel
->outbound
, &debuginfo
->outbound
);
107 * vmbus_open - Open the specified channel.
109 int vmbus_open(struct vmbus_channel
*newchannel
, u32 send_ringbuffer_size
,
110 u32 recv_ringbuffer_size
, void *userdata
, u32 userdatalen
,
111 void (*onchannelcallback
)(void *context
), void *context
)
113 struct vmbus_channel_open_channel
*open_msg
;
114 struct vmbus_channel_msginfo
*open_info
= NULL
;
119 spin_lock_irqsave(&newchannel
->sc_lock
, flags
);
120 if (newchannel
->state
== CHANNEL_OPEN_STATE
) {
121 newchannel
->state
= CHANNEL_OPENING_STATE
;
123 spin_unlock_irqrestore(&newchannel
->sc_lock
, flags
);
126 spin_unlock_irqrestore(&newchannel
->sc_lock
, flags
);
128 newchannel
->onchannel_callback
= onchannelcallback
;
129 newchannel
->channel_callback_context
= context
;
131 /* Allocate the ring buffer */
132 out
= (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
133 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
139 in
= (void *)((unsigned long)out
+ send_ringbuffer_size
);
141 newchannel
->ringbuffer_pages
= out
;
142 newchannel
->ringbuffer_pagecount
= (send_ringbuffer_size
+
143 recv_ringbuffer_size
) >> PAGE_SHIFT
;
145 ret
= hv_ringbuffer_init(
146 &newchannel
->outbound
, out
, send_ringbuffer_size
);
153 ret
= hv_ringbuffer_init(
154 &newchannel
->inbound
, in
, recv_ringbuffer_size
);
161 /* Establish the gpadl for the ring buffer */
162 newchannel
->ringbuffer_gpadlhandle
= 0;
164 ret
= vmbus_establish_gpadl(newchannel
,
165 newchannel
->outbound
.ring_buffer
,
166 send_ringbuffer_size
+
167 recv_ringbuffer_size
,
168 &newchannel
->ringbuffer_gpadlhandle
);
175 /* Create and init the channel open message */
176 open_info
= kmalloc(sizeof(*open_info
) +
177 sizeof(struct vmbus_channel_open_channel
),
184 init_completion(&open_info
->waitevent
);
186 open_msg
= (struct vmbus_channel_open_channel
*)open_info
->msg
;
187 open_msg
->header
.msgtype
= CHANNELMSG_OPENCHANNEL
;
188 open_msg
->openid
= newchannel
->offermsg
.child_relid
;
189 open_msg
->child_relid
= newchannel
->offermsg
.child_relid
;
190 open_msg
->ringbuffer_gpadlhandle
= newchannel
->ringbuffer_gpadlhandle
;
191 open_msg
->downstream_ringbuffer_pageoffset
= send_ringbuffer_size
>>
193 open_msg
->target_vp
= newchannel
->target_vp
;
195 if (userdatalen
> MAX_USER_DEFINED_BYTES
) {
201 memcpy(open_msg
->userdata
, userdata
, userdatalen
);
203 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
204 list_add_tail(&open_info
->msglistentry
,
205 &vmbus_connection
.chn_msg_list
);
206 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
208 ret
= vmbus_post_msg(open_msg
,
209 sizeof(struct vmbus_channel_open_channel
));
216 t
= wait_for_completion_timeout(&open_info
->waitevent
, 5*HZ
);
223 if (open_info
->response
.open_result
.status
)
224 err
= open_info
->response
.open_result
.status
;
226 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
227 list_del(&open_info
->msglistentry
);
228 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
231 newchannel
->state
= CHANNEL_OPENED_STATE
;
237 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
238 list_del(&open_info
->msglistentry
);
239 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
242 vmbus_teardown_gpadl(newchannel
, newchannel
->ringbuffer_gpadlhandle
);
245 free_pages((unsigned long)out
,
246 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
250 EXPORT_SYMBOL_GPL(vmbus_open
);
253 * create_gpadl_header - Creates a gpadl for the specified buffer
255 static int create_gpadl_header(void *kbuffer
, u32 size
,
256 struct vmbus_channel_msginfo
**msginfo
,
261 unsigned long long pfn
;
262 struct vmbus_channel_gpadl_header
*gpadl_header
;
263 struct vmbus_channel_gpadl_body
*gpadl_body
;
264 struct vmbus_channel_msginfo
*msgheader
;
265 struct vmbus_channel_msginfo
*msgbody
= NULL
;
268 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
270 pagecount
= size
>> PAGE_SHIFT
;
271 pfn
= virt_to_phys(kbuffer
) >> PAGE_SHIFT
;
273 /* do we need a gpadl body msg */
274 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
275 sizeof(struct vmbus_channel_gpadl_header
) -
276 sizeof(struct gpa_range
);
277 pfncount
= pfnsize
/ sizeof(u64
);
279 if (pagecount
> pfncount
) {
280 /* we need a gpadl body */
281 /* fill in the header */
282 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
283 sizeof(struct vmbus_channel_gpadl_header
) +
284 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
285 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
289 INIT_LIST_HEAD(&msgheader
->submsglist
);
290 msgheader
->msgsize
= msgsize
;
292 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
294 gpadl_header
->rangecount
= 1;
295 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
296 pagecount
* sizeof(u64
);
297 gpadl_header
->range
[0].byte_offset
= 0;
298 gpadl_header
->range
[0].byte_count
= size
;
299 for (i
= 0; i
< pfncount
; i
++)
300 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
301 *msginfo
= msgheader
;
305 pfnleft
= pagecount
- pfncount
;
307 /* how many pfns can we fit */
308 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
309 sizeof(struct vmbus_channel_gpadl_body
);
310 pfncount
= pfnsize
/ sizeof(u64
);
312 /* fill in the body */
314 if (pfnleft
> pfncount
)
319 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
320 sizeof(struct vmbus_channel_gpadl_body
) +
321 pfncurr
* sizeof(u64
);
322 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
325 struct vmbus_channel_msginfo
*pos
= NULL
;
326 struct vmbus_channel_msginfo
*tmp
= NULL
;
328 * Free up all the allocated messages.
330 list_for_each_entry_safe(pos
, tmp
,
331 &msgheader
->submsglist
,
334 list_del(&pos
->msglistentry
);
341 msgbody
->msgsize
= msgsize
;
344 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
347 * Gpadl is u32 and we are using a pointer which could
349 * This is governed by the guest/host protocol and
350 * so the hypervisor gurantees that this is ok.
352 for (i
= 0; i
< pfncurr
; i
++)
353 gpadl_body
->pfn
[i
] = pfn
+ pfnsum
+ i
;
355 /* add to msg header */
356 list_add_tail(&msgbody
->msglistentry
,
357 &msgheader
->submsglist
);
362 /* everything fits in a header */
363 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
364 sizeof(struct vmbus_channel_gpadl_header
) +
365 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
366 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
367 if (msgheader
== NULL
)
369 msgheader
->msgsize
= msgsize
;
371 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
373 gpadl_header
->rangecount
= 1;
374 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
375 pagecount
* sizeof(u64
);
376 gpadl_header
->range
[0].byte_offset
= 0;
377 gpadl_header
->range
[0].byte_count
= size
;
378 for (i
= 0; i
< pagecount
; i
++)
379 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
381 *msginfo
= msgheader
;
393 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
395 * @channel: a channel
396 * @kbuffer: from kmalloc
397 * @size: page-size multiple
398 * @gpadl_handle: some funky thing
400 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
401 u32 size
, u32
*gpadl_handle
)
403 struct vmbus_channel_gpadl_header
*gpadlmsg
;
404 struct vmbus_channel_gpadl_body
*gpadl_body
;
405 struct vmbus_channel_msginfo
*msginfo
= NULL
;
406 struct vmbus_channel_msginfo
*submsginfo
;
408 struct list_head
*curr
;
409 u32 next_gpadl_handle
;
413 next_gpadl_handle
= atomic_read(&vmbus_connection
.next_gpadl_handle
);
414 atomic_inc(&vmbus_connection
.next_gpadl_handle
);
416 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
, &msgcount
);
420 init_completion(&msginfo
->waitevent
);
422 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
423 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
424 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
425 gpadlmsg
->gpadl
= next_gpadl_handle
;
428 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
429 list_add_tail(&msginfo
->msglistentry
,
430 &vmbus_connection
.chn_msg_list
);
432 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
434 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
440 list_for_each(curr
, &msginfo
->submsglist
) {
442 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
444 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
446 gpadl_body
->header
.msgtype
=
447 CHANNELMSG_GPADL_BODY
;
448 gpadl_body
->gpadl
= next_gpadl_handle
;
450 ret
= vmbus_post_msg(gpadl_body
,
451 submsginfo
->msgsize
-
452 sizeof(*submsginfo
));
458 wait_for_completion(&msginfo
->waitevent
);
460 /* At this point, we received the gpadl created msg */
461 *gpadl_handle
= gpadlmsg
->gpadl
;
464 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
465 list_del(&msginfo
->msglistentry
);
466 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
471 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
474 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
476 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
478 struct vmbus_channel_gpadl_teardown
*msg
;
479 struct vmbus_channel_msginfo
*info
;
483 info
= kmalloc(sizeof(*info
) +
484 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
488 init_completion(&info
->waitevent
);
490 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
492 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
493 msg
->child_relid
= channel
->offermsg
.child_relid
;
494 msg
->gpadl
= gpadl_handle
;
496 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
497 list_add_tail(&info
->msglistentry
,
498 &vmbus_connection
.chn_msg_list
);
499 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
500 ret
= vmbus_post_msg(msg
,
501 sizeof(struct vmbus_channel_gpadl_teardown
));
506 wait_for_completion(&info
->waitevent
);
509 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
510 list_del(&info
->msglistentry
);
511 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
516 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
518 static int vmbus_close_internal(struct vmbus_channel
*channel
)
520 struct vmbus_channel_close_channel
*msg
;
524 channel
->state
= CHANNEL_OPEN_STATE
;
525 channel
->sc_creation_callback
= NULL
;
526 /* Stop callback and cancel the timer asap */
527 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
528 channel
->onchannel_callback
= NULL
;
529 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
531 /* Send a closing message */
533 msg
= &channel
->close_msg
.msg
;
535 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
536 msg
->child_relid
= channel
->offermsg
.child_relid
;
538 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
));
541 pr_err("Close failed: close post msg return is %d\n", ret
);
543 * If we failed to post the close msg,
544 * it is perhaps better to leak memory.
549 /* Tear down the gpadl for the channel's ring buffer */
550 if (channel
->ringbuffer_gpadlhandle
) {
551 ret
= vmbus_teardown_gpadl(channel
,
552 channel
->ringbuffer_gpadlhandle
);
554 pr_err("Close failed: teardown gpadl return %d\n", ret
);
556 * If we failed to teardown gpadl,
557 * it is perhaps better to leak memory.
563 /* Cleanup the ring buffers for this channel */
564 hv_ringbuffer_cleanup(&channel
->outbound
);
565 hv_ringbuffer_cleanup(&channel
->inbound
);
567 free_pages((unsigned long)channel
->ringbuffer_pages
,
568 get_order(channel
->ringbuffer_pagecount
* PAGE_SIZE
));
574 * vmbus_close - Close the specified channel
576 void vmbus_close(struct vmbus_channel
*channel
)
578 struct list_head
*cur
, *tmp
;
579 struct vmbus_channel
*cur_channel
;
581 if (channel
->primary_channel
!= NULL
) {
583 * We will only close sub-channels when
584 * the primary is closed.
589 * Close all the sub-channels first and then close the
592 list_for_each_safe(cur
, tmp
, &channel
->sc_list
) {
593 cur_channel
= list_entry(cur
, struct vmbus_channel
, sc_list
);
594 if (cur_channel
->state
!= CHANNEL_OPENED_STATE
)
596 vmbus_close_internal(cur_channel
);
599 * Now close the primary.
601 vmbus_close_internal(channel
);
603 EXPORT_SYMBOL_GPL(vmbus_close
);
606 * vmbus_sendpacket() - Send the specified buffer on the given channel
607 * @channel: Pointer to vmbus_channel structure.
608 * @buffer: Pointer to the buffer you want to receive the data into.
609 * @bufferlen: Maximum size of what the the buffer will hold
610 * @requestid: Identifier of the request
611 * @type: Type of packet that is being send e.g. negotiate, time
614 * Sends data in @buffer directly to hyper-v via the vmbus
615 * This will send the data unparsed to hyper-v.
617 * Mainly used by Hyper-V drivers.
619 int vmbus_sendpacket(struct vmbus_channel
*channel
, const void *buffer
,
620 u32 bufferlen
, u64 requestid
,
621 enum vmbus_packet_type type
, u32 flags
)
623 struct vmpacket_descriptor desc
;
624 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
625 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
626 struct scatterlist bufferlist
[3];
627 u64 aligned_data
= 0;
632 /* Setup the descriptor */
633 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
634 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
635 /* in 8-bytes granularity */
636 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
637 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
638 desc
.trans_id
= requestid
;
640 sg_init_table(bufferlist
, 3);
641 sg_set_buf(&bufferlist
[0], &desc
, sizeof(struct vmpacket_descriptor
));
642 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
643 sg_set_buf(&bufferlist
[2], &aligned_data
,
644 packetlen_aligned
- packetlen
);
646 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
648 if (ret
== 0 && signal
)
649 vmbus_setevent(channel
);
653 EXPORT_SYMBOL(vmbus_sendpacket
);
656 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
657 * packets using a GPADL Direct packet type.
659 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
660 struct hv_page_buffer pagebuffers
[],
661 u32 pagecount
, void *buffer
, u32 bufferlen
,
666 struct vmbus_channel_packet_page_buffer desc
;
669 u32 packetlen_aligned
;
670 struct scatterlist bufferlist
[3];
671 u64 aligned_data
= 0;
674 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
679 * Adjust the size down since vmbus_channel_packet_page_buffer is the
680 * largest size we support
682 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
683 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
684 sizeof(struct hv_page_buffer
));
685 packetlen
= descsize
+ bufferlen
;
686 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
688 /* Setup the descriptor */
689 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
690 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
691 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
692 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
693 desc
.transactionid
= requestid
;
694 desc
.rangecount
= pagecount
;
696 for (i
= 0; i
< pagecount
; i
++) {
697 desc
.range
[i
].len
= pagebuffers
[i
].len
;
698 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
699 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
702 sg_init_table(bufferlist
, 3);
703 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
704 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
705 sg_set_buf(&bufferlist
[2], &aligned_data
,
706 packetlen_aligned
- packetlen
);
708 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
710 if (ret
== 0 && signal
)
711 vmbus_setevent(channel
);
715 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
718 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
719 * using a GPADL Direct packet type.
721 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
722 struct hv_multipage_buffer
*multi_pagebuffer
,
723 void *buffer
, u32 bufferlen
, u64 requestid
)
726 struct vmbus_channel_packet_multipage_buffer desc
;
729 u32 packetlen_aligned
;
730 struct scatterlist bufferlist
[3];
731 u64 aligned_data
= 0;
733 u32 pfncount
= NUM_PAGES_SPANNED(multi_pagebuffer
->offset
,
734 multi_pagebuffer
->len
);
737 if ((pfncount
< 0) || (pfncount
> MAX_MULTIPAGE_BUFFER_COUNT
))
741 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
742 * the largest size we support
744 descsize
= sizeof(struct vmbus_channel_packet_multipage_buffer
) -
745 ((MAX_MULTIPAGE_BUFFER_COUNT
- pfncount
) *
747 packetlen
= descsize
+ bufferlen
;
748 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
751 /* Setup the descriptor */
752 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
753 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
754 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
755 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
756 desc
.transactionid
= requestid
;
759 desc
.range
.len
= multi_pagebuffer
->len
;
760 desc
.range
.offset
= multi_pagebuffer
->offset
;
762 memcpy(desc
.range
.pfn_array
, multi_pagebuffer
->pfn_array
,
763 pfncount
* sizeof(u64
));
765 sg_init_table(bufferlist
, 3);
766 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
767 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
768 sg_set_buf(&bufferlist
[2], &aligned_data
,
769 packetlen_aligned
- packetlen
);
771 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
773 if (ret
== 0 && signal
)
774 vmbus_setevent(channel
);
778 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer
);
781 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
782 * @channel: Pointer to vmbus_channel structure.
783 * @buffer: Pointer to the buffer you want to receive the data into.
784 * @bufferlen: Maximum size of what the the buffer will hold
785 * @buffer_actual_len: The actual size of the data after it was received
786 * @requestid: Identifier of the request
788 * Receives directly from the hyper-v vmbus and puts the data it received
789 * into Buffer. This will receive the data unparsed from hyper-v.
791 * Mainly used by Hyper-V drivers.
793 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
794 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
)
796 struct vmpacket_descriptor desc
;
802 *buffer_actual_len
= 0;
806 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
807 sizeof(struct vmpacket_descriptor
));
811 packetlen
= desc
.len8
<< 3;
812 userlen
= packetlen
- (desc
.offset8
<< 3);
814 *buffer_actual_len
= userlen
;
816 if (userlen
> bufferlen
) {
818 pr_err("Buffer too small - got %d needs %d\n",
823 *requestid
= desc
.trans_id
;
825 /* Copy over the packet to the user buffer */
826 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, userlen
,
827 (desc
.offset8
<< 3), &signal
);
830 vmbus_setevent(channel
);
834 EXPORT_SYMBOL(vmbus_recvpacket
);
837 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
839 int vmbus_recvpacket_raw(struct vmbus_channel
*channel
, void *buffer
,
840 u32 bufferlen
, u32
*buffer_actual_len
,
843 struct vmpacket_descriptor desc
;
848 *buffer_actual_len
= 0;
852 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
853 sizeof(struct vmpacket_descriptor
));
858 packetlen
= desc
.len8
<< 3;
860 *buffer_actual_len
= packetlen
;
862 if (packetlen
> bufferlen
) {
863 pr_err("Buffer too small - needed %d bytes but "
864 "got space for only %d bytes\n",
865 packetlen
, bufferlen
);
869 *requestid
= desc
.trans_id
;
871 /* Copy over the entire packet to the user buffer */
872 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, packetlen
, 0,
876 vmbus_setevent(channel
);
880 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);