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>
31 #include <linux/interrupt.h>
33 #include "hyperv_vmbus.h"
35 #define NUM_PAGES_SPANNED(addr, len) \
36 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
39 * vmbus_setevent- Trigger an event notification on the specified
42 void vmbus_setevent(struct vmbus_channel
*channel
)
44 struct hv_monitor_page
*monitorpage
;
47 * For channels marked as in "low latency" mode
48 * bypass the monitor page mechanism.
50 if ((channel
->offermsg
.monitor_allocated
) &&
51 (!channel
->low_latency
)) {
52 /* Each u32 represents 32 channels */
53 sync_set_bit(channel
->offermsg
.child_relid
& 31,
54 (unsigned long *) vmbus_connection
.send_int_page
+
55 (channel
->offermsg
.child_relid
>> 5));
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
);
71 * vmbus_open - Open the specified channel.
73 int vmbus_open(struct vmbus_channel
*newchannel
, u32 send_ringbuffer_size
,
74 u32 recv_ringbuffer_size
, void *userdata
, u32 userdatalen
,
75 void (*onchannelcallback
)(void *context
), void *context
)
77 struct vmbus_channel_open_channel
*open_msg
;
78 struct vmbus_channel_msginfo
*open_info
= NULL
;
83 if (send_ringbuffer_size
% PAGE_SIZE
||
84 recv_ringbuffer_size
% PAGE_SIZE
)
87 spin_lock_irqsave(&newchannel
->lock
, flags
);
88 if (newchannel
->state
== CHANNEL_OPEN_STATE
) {
89 newchannel
->state
= CHANNEL_OPENING_STATE
;
91 spin_unlock_irqrestore(&newchannel
->lock
, flags
);
94 spin_unlock_irqrestore(&newchannel
->lock
, flags
);
96 newchannel
->onchannel_callback
= onchannelcallback
;
97 newchannel
->channel_callback_context
= context
;
99 /* Allocate the ring buffer */
100 page
= alloc_pages_node(cpu_to_node(newchannel
->target_cpu
),
101 GFP_KERNEL
|__GFP_ZERO
,
102 get_order(send_ringbuffer_size
+
103 recv_ringbuffer_size
));
106 page
= alloc_pages(GFP_KERNEL
|__GFP_ZERO
,
107 get_order(send_ringbuffer_size
+
108 recv_ringbuffer_size
));
112 goto error_set_chnstate
;
115 newchannel
->ringbuffer_pages
= page_address(page
);
116 newchannel
->ringbuffer_pagecount
= (send_ringbuffer_size
+
117 recv_ringbuffer_size
) >> PAGE_SHIFT
;
119 ret
= hv_ringbuffer_init(&newchannel
->outbound
, page
,
120 send_ringbuffer_size
>> PAGE_SHIFT
);
124 goto error_free_pages
;
127 ret
= hv_ringbuffer_init(&newchannel
->inbound
,
128 &page
[send_ringbuffer_size
>> PAGE_SHIFT
],
129 recv_ringbuffer_size
>> PAGE_SHIFT
);
132 goto error_free_pages
;
136 /* Establish the gpadl for the ring buffer */
137 newchannel
->ringbuffer_gpadlhandle
= 0;
139 ret
= vmbus_establish_gpadl(newchannel
,
141 send_ringbuffer_size
+
142 recv_ringbuffer_size
,
143 &newchannel
->ringbuffer_gpadlhandle
);
147 goto error_free_pages
;
150 /* Create and init the channel open message */
151 open_info
= kmalloc(sizeof(*open_info
) +
152 sizeof(struct vmbus_channel_open_channel
),
156 goto error_free_gpadl
;
159 init_completion(&open_info
->waitevent
);
161 open_msg
= (struct vmbus_channel_open_channel
*)open_info
->msg
;
162 open_msg
->header
.msgtype
= CHANNELMSG_OPENCHANNEL
;
163 open_msg
->openid
= newchannel
->offermsg
.child_relid
;
164 open_msg
->child_relid
= newchannel
->offermsg
.child_relid
;
165 open_msg
->ringbuffer_gpadlhandle
= newchannel
->ringbuffer_gpadlhandle
;
166 open_msg
->downstream_ringbuffer_pageoffset
= send_ringbuffer_size
>>
168 open_msg
->target_vp
= newchannel
->target_vp
;
170 if (userdatalen
> MAX_USER_DEFINED_BYTES
) {
172 goto error_free_gpadl
;
176 memcpy(open_msg
->userdata
, userdata
, userdatalen
);
178 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
179 list_add_tail(&open_info
->msglistentry
,
180 &vmbus_connection
.chn_msg_list
);
181 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
183 ret
= vmbus_post_msg(open_msg
,
184 sizeof(struct vmbus_channel_open_channel
));
188 goto error_clean_msglist
;
191 wait_for_completion(&open_info
->waitevent
);
193 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
194 list_del(&open_info
->msglistentry
);
195 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
197 if (open_info
->response
.open_result
.status
) {
199 goto error_free_gpadl
;
202 newchannel
->state
= CHANNEL_OPENED_STATE
;
207 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
208 list_del(&open_info
->msglistentry
);
209 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
212 vmbus_teardown_gpadl(newchannel
, newchannel
->ringbuffer_gpadlhandle
);
215 hv_ringbuffer_cleanup(&newchannel
->outbound
);
216 hv_ringbuffer_cleanup(&newchannel
->inbound
);
218 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
220 newchannel
->state
= CHANNEL_OPEN_STATE
;
223 EXPORT_SYMBOL_GPL(vmbus_open
);
225 /* Used for Hyper-V Socket: a guest client's connect() to the host */
226 int vmbus_send_tl_connect_request(const uuid_le
*shv_guest_servie_id
,
227 const uuid_le
*shv_host_servie_id
)
229 struct vmbus_channel_tl_connect_request conn_msg
;
231 memset(&conn_msg
, 0, sizeof(conn_msg
));
232 conn_msg
.header
.msgtype
= CHANNELMSG_TL_CONNECT_REQUEST
;
233 conn_msg
.guest_endpoint_id
= *shv_guest_servie_id
;
234 conn_msg
.host_service_id
= *shv_host_servie_id
;
236 return vmbus_post_msg(&conn_msg
, sizeof(conn_msg
));
238 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request
);
241 * create_gpadl_header - Creates a gpadl for the specified buffer
243 static int create_gpadl_header(void *kbuffer
, u32 size
,
244 struct vmbus_channel_msginfo
**msginfo
)
248 struct vmbus_channel_gpadl_header
*gpadl_header
;
249 struct vmbus_channel_gpadl_body
*gpadl_body
;
250 struct vmbus_channel_msginfo
*msgheader
;
251 struct vmbus_channel_msginfo
*msgbody
= NULL
;
254 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
256 pagecount
= size
>> PAGE_SHIFT
;
258 /* do we need a gpadl body msg */
259 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
260 sizeof(struct vmbus_channel_gpadl_header
) -
261 sizeof(struct gpa_range
);
262 pfncount
= pfnsize
/ sizeof(u64
);
264 if (pagecount
> pfncount
) {
265 /* we need a gpadl body */
266 /* fill in the header */
267 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
268 sizeof(struct vmbus_channel_gpadl_header
) +
269 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
270 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
274 INIT_LIST_HEAD(&msgheader
->submsglist
);
275 msgheader
->msgsize
= msgsize
;
277 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
279 gpadl_header
->rangecount
= 1;
280 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
281 pagecount
* sizeof(u64
);
282 gpadl_header
->range
[0].byte_offset
= 0;
283 gpadl_header
->range
[0].byte_count
= size
;
284 for (i
= 0; i
< pfncount
; i
++)
285 gpadl_header
->range
[0].pfn_array
[i
] = slow_virt_to_phys(
286 kbuffer
+ PAGE_SIZE
* i
) >> PAGE_SHIFT
;
287 *msginfo
= msgheader
;
290 pfnleft
= pagecount
- pfncount
;
292 /* how many pfns can we fit */
293 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
294 sizeof(struct vmbus_channel_gpadl_body
);
295 pfncount
= pfnsize
/ sizeof(u64
);
297 /* fill in the body */
299 if (pfnleft
> pfncount
)
304 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
305 sizeof(struct vmbus_channel_gpadl_body
) +
306 pfncurr
* sizeof(u64
);
307 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
310 struct vmbus_channel_msginfo
*pos
= NULL
;
311 struct vmbus_channel_msginfo
*tmp
= NULL
;
313 * Free up all the allocated messages.
315 list_for_each_entry_safe(pos
, tmp
,
316 &msgheader
->submsglist
,
319 list_del(&pos
->msglistentry
);
326 msgbody
->msgsize
= msgsize
;
328 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
331 * Gpadl is u32 and we are using a pointer which could
333 * This is governed by the guest/host protocol and
334 * so the hypervisor gurantees that this is ok.
336 for (i
= 0; i
< pfncurr
; i
++)
337 gpadl_body
->pfn
[i
] = slow_virt_to_phys(
338 kbuffer
+ PAGE_SIZE
* (pfnsum
+ i
)) >>
341 /* add to msg header */
342 list_add_tail(&msgbody
->msglistentry
,
343 &msgheader
->submsglist
);
348 /* everything fits in a header */
349 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
350 sizeof(struct vmbus_channel_gpadl_header
) +
351 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
352 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
353 if (msgheader
== NULL
)
356 INIT_LIST_HEAD(&msgheader
->submsglist
);
357 msgheader
->msgsize
= msgsize
;
359 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
361 gpadl_header
->rangecount
= 1;
362 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
363 pagecount
* sizeof(u64
);
364 gpadl_header
->range
[0].byte_offset
= 0;
365 gpadl_header
->range
[0].byte_count
= size
;
366 for (i
= 0; i
< pagecount
; i
++)
367 gpadl_header
->range
[0].pfn_array
[i
] = slow_virt_to_phys(
368 kbuffer
+ PAGE_SIZE
* i
) >> PAGE_SHIFT
;
370 *msginfo
= msgheader
;
381 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
383 * @channel: a channel
384 * @kbuffer: from kmalloc or vmalloc
385 * @size: page-size multiple
386 * @gpadl_handle: some funky thing
388 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
389 u32 size
, u32
*gpadl_handle
)
391 struct vmbus_channel_gpadl_header
*gpadlmsg
;
392 struct vmbus_channel_gpadl_body
*gpadl_body
;
393 struct vmbus_channel_msginfo
*msginfo
= NULL
;
394 struct vmbus_channel_msginfo
*submsginfo
, *tmp
;
395 struct list_head
*curr
;
396 u32 next_gpadl_handle
;
401 (atomic_inc_return(&vmbus_connection
.next_gpadl_handle
) - 1);
403 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
);
407 init_completion(&msginfo
->waitevent
);
409 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
410 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
411 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
412 gpadlmsg
->gpadl
= next_gpadl_handle
;
415 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
416 list_add_tail(&msginfo
->msglistentry
,
417 &vmbus_connection
.chn_msg_list
);
419 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
421 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
426 list_for_each(curr
, &msginfo
->submsglist
) {
427 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
429 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
431 gpadl_body
->header
.msgtype
=
432 CHANNELMSG_GPADL_BODY
;
433 gpadl_body
->gpadl
= next_gpadl_handle
;
435 ret
= vmbus_post_msg(gpadl_body
,
436 submsginfo
->msgsize
-
437 sizeof(*submsginfo
));
442 wait_for_completion(&msginfo
->waitevent
);
444 /* At this point, we received the gpadl created msg */
445 *gpadl_handle
= gpadlmsg
->gpadl
;
448 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
449 list_del(&msginfo
->msglistentry
);
450 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
451 list_for_each_entry_safe(submsginfo
, tmp
, &msginfo
->submsglist
,
459 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
462 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
464 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
466 struct vmbus_channel_gpadl_teardown
*msg
;
467 struct vmbus_channel_msginfo
*info
;
471 info
= kmalloc(sizeof(*info
) +
472 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
476 init_completion(&info
->waitevent
);
478 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
480 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
481 msg
->child_relid
= channel
->offermsg
.child_relid
;
482 msg
->gpadl
= gpadl_handle
;
484 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
485 list_add_tail(&info
->msglistentry
,
486 &vmbus_connection
.chn_msg_list
);
487 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
488 ret
= vmbus_post_msg(msg
,
489 sizeof(struct vmbus_channel_gpadl_teardown
));
494 wait_for_completion(&info
->waitevent
);
497 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
498 list_del(&info
->msglistentry
);
499 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
504 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
506 static void reset_channel_cb(void *arg
)
508 struct vmbus_channel
*channel
= arg
;
510 channel
->onchannel_callback
= NULL
;
513 static int vmbus_close_internal(struct vmbus_channel
*channel
)
515 struct vmbus_channel_close_channel
*msg
;
519 * process_chn_event(), running in the tasklet, can race
520 * with vmbus_close_internal() in the case of SMP guest, e.g., when
521 * the former is accessing channel->inbound.ring_buffer, the latter
522 * could be freeing the ring_buffer pages.
524 * To resolve the race, we can serialize them by disabling the
525 * tasklet when the latter is running here.
527 hv_event_tasklet_disable(channel
);
530 * In case a device driver's probe() fails (e.g.,
531 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
532 * rescinded later (e.g., we dynamically disble an Integrated Service
533 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
534 * here we should skip most of the below cleanup work.
536 if (channel
->state
!= CHANNEL_OPENED_STATE
) {
541 channel
->state
= CHANNEL_OPEN_STATE
;
542 channel
->sc_creation_callback
= NULL
;
543 /* Stop callback and cancel the timer asap */
544 if (channel
->target_cpu
!= get_cpu()) {
546 smp_call_function_single(channel
->target_cpu
, reset_channel_cb
,
549 reset_channel_cb(channel
);
553 /* Send a closing message */
555 msg
= &channel
->close_msg
.msg
;
557 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
558 msg
->child_relid
= channel
->offermsg
.child_relid
;
560 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
));
563 pr_err("Close failed: close post msg return is %d\n", ret
);
565 * If we failed to post the close msg,
566 * it is perhaps better to leak memory.
571 /* Tear down the gpadl for the channel's ring buffer */
572 if (channel
->ringbuffer_gpadlhandle
) {
573 ret
= vmbus_teardown_gpadl(channel
,
574 channel
->ringbuffer_gpadlhandle
);
576 pr_err("Close failed: teardown gpadl return %d\n", ret
);
578 * If we failed to teardown gpadl,
579 * it is perhaps better to leak memory.
585 /* Cleanup the ring buffers for this channel */
586 hv_ringbuffer_cleanup(&channel
->outbound
);
587 hv_ringbuffer_cleanup(&channel
->inbound
);
589 free_pages((unsigned long)channel
->ringbuffer_pages
,
590 get_order(channel
->ringbuffer_pagecount
* PAGE_SIZE
));
593 hv_event_tasklet_enable(channel
);
599 * vmbus_close - Close the specified channel
601 void vmbus_close(struct vmbus_channel
*channel
)
603 struct list_head
*cur
, *tmp
;
604 struct vmbus_channel
*cur_channel
;
606 if (channel
->primary_channel
!= NULL
) {
608 * We will only close sub-channels when
609 * the primary is closed.
614 * Close all the sub-channels first and then close the
617 list_for_each_safe(cur
, tmp
, &channel
->sc_list
) {
618 cur_channel
= list_entry(cur
, struct vmbus_channel
, sc_list
);
619 if (cur_channel
->state
!= CHANNEL_OPENED_STATE
)
621 vmbus_close_internal(cur_channel
);
624 * Now close the primary.
626 vmbus_close_internal(channel
);
628 EXPORT_SYMBOL_GPL(vmbus_close
);
630 int vmbus_sendpacket_ctl(struct vmbus_channel
*channel
, void *buffer
,
631 u32 bufferlen
, u64 requestid
,
632 enum vmbus_packet_type type
, u32 flags
, bool kick_q
)
634 struct vmpacket_descriptor desc
;
635 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
636 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
637 struct kvec bufferlist
[3];
638 u64 aligned_data
= 0;
639 bool lock
= channel
->acquire_ring_lock
;
640 int num_vecs
= ((bufferlen
!= 0) ? 3 : 1);
643 /* Setup the descriptor */
644 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
645 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
646 /* in 8-bytes granularity */
647 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
648 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
649 desc
.trans_id
= requestid
;
651 bufferlist
[0].iov_base
= &desc
;
652 bufferlist
[0].iov_len
= sizeof(struct vmpacket_descriptor
);
653 bufferlist
[1].iov_base
= buffer
;
654 bufferlist
[1].iov_len
= bufferlen
;
655 bufferlist
[2].iov_base
= &aligned_data
;
656 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
658 return hv_ringbuffer_write(channel
, bufferlist
, num_vecs
,
662 EXPORT_SYMBOL(vmbus_sendpacket_ctl
);
665 * vmbus_sendpacket() - Send the specified buffer on the given channel
666 * @channel: Pointer to vmbus_channel structure.
667 * @buffer: Pointer to the buffer you want to receive the data into.
668 * @bufferlen: Maximum size of what the the buffer will hold
669 * @requestid: Identifier of the request
670 * @type: Type of packet that is being send e.g. negotiate, time
673 * Sends data in @buffer directly to hyper-v via the vmbus
674 * This will send the data unparsed to hyper-v.
676 * Mainly used by Hyper-V drivers.
678 int vmbus_sendpacket(struct vmbus_channel
*channel
, void *buffer
,
679 u32 bufferlen
, u64 requestid
,
680 enum vmbus_packet_type type
, u32 flags
)
682 return vmbus_sendpacket_ctl(channel
, buffer
, bufferlen
, requestid
,
685 EXPORT_SYMBOL(vmbus_sendpacket
);
688 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
689 * packets using a GPADL Direct packet type. This interface allows you
690 * to control notifying the host. This will be useful for sending
691 * batched data. Also the sender can control the send flags
694 int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel
*channel
,
695 struct hv_page_buffer pagebuffers
[],
696 u32 pagecount
, void *buffer
, u32 bufferlen
,
702 struct vmbus_channel_packet_page_buffer desc
;
705 u32 packetlen_aligned
;
706 struct kvec bufferlist
[3];
707 u64 aligned_data
= 0;
708 bool lock
= channel
->acquire_ring_lock
;
710 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
715 * Adjust the size down since vmbus_channel_packet_page_buffer is the
716 * largest size we support
718 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
719 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
720 sizeof(struct hv_page_buffer
));
721 packetlen
= descsize
+ bufferlen
;
722 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
724 /* Setup the descriptor */
725 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
727 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
728 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
729 desc
.transactionid
= requestid
;
730 desc
.rangecount
= pagecount
;
732 for (i
= 0; i
< pagecount
; i
++) {
733 desc
.range
[i
].len
= pagebuffers
[i
].len
;
734 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
735 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
738 bufferlist
[0].iov_base
= &desc
;
739 bufferlist
[0].iov_len
= descsize
;
740 bufferlist
[1].iov_base
= buffer
;
741 bufferlist
[1].iov_len
= bufferlen
;
742 bufferlist
[2].iov_base
= &aligned_data
;
743 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
745 return hv_ringbuffer_write(channel
, bufferlist
, 3,
748 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl
);
751 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
752 * packets using a GPADL Direct packet type.
754 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
755 struct hv_page_buffer pagebuffers
[],
756 u32 pagecount
, void *buffer
, u32 bufferlen
,
759 u32 flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
760 return vmbus_sendpacket_pagebuffer_ctl(channel
, pagebuffers
, pagecount
,
761 buffer
, bufferlen
, requestid
,
765 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
768 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
769 * using a GPADL Direct packet type.
770 * The buffer includes the vmbus descriptor.
772 int vmbus_sendpacket_mpb_desc(struct vmbus_channel
*channel
,
773 struct vmbus_packet_mpb_array
*desc
,
775 void *buffer
, u32 bufferlen
, u64 requestid
)
778 u32 packetlen_aligned
;
779 struct kvec bufferlist
[3];
780 u64 aligned_data
= 0;
781 bool lock
= channel
->acquire_ring_lock
;
783 packetlen
= desc_size
+ bufferlen
;
784 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
786 /* Setup the descriptor */
787 desc
->type
= VM_PKT_DATA_USING_GPA_DIRECT
;
788 desc
->flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
789 desc
->dataoffset8
= desc_size
>> 3; /* in 8-bytes grandularity */
790 desc
->length8
= (u16
)(packetlen_aligned
>> 3);
791 desc
->transactionid
= requestid
;
792 desc
->rangecount
= 1;
794 bufferlist
[0].iov_base
= desc
;
795 bufferlist
[0].iov_len
= desc_size
;
796 bufferlist
[1].iov_base
= buffer
;
797 bufferlist
[1].iov_len
= bufferlen
;
798 bufferlist
[2].iov_base
= &aligned_data
;
799 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
801 return hv_ringbuffer_write(channel
, bufferlist
, 3,
804 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc
);
807 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
808 * using a GPADL Direct packet type.
810 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
811 struct hv_multipage_buffer
*multi_pagebuffer
,
812 void *buffer
, u32 bufferlen
, u64 requestid
)
814 struct vmbus_channel_packet_multipage_buffer desc
;
817 u32 packetlen_aligned
;
818 struct kvec bufferlist
[3];
819 u64 aligned_data
= 0;
820 bool lock
= channel
->acquire_ring_lock
;
821 u32 pfncount
= NUM_PAGES_SPANNED(multi_pagebuffer
->offset
,
822 multi_pagebuffer
->len
);
824 if (pfncount
> MAX_MULTIPAGE_BUFFER_COUNT
)
828 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
829 * the largest size we support
831 descsize
= sizeof(struct vmbus_channel_packet_multipage_buffer
) -
832 ((MAX_MULTIPAGE_BUFFER_COUNT
- pfncount
) *
834 packetlen
= descsize
+ bufferlen
;
835 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
838 /* Setup the descriptor */
839 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
840 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
841 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
842 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
843 desc
.transactionid
= requestid
;
846 desc
.range
.len
= multi_pagebuffer
->len
;
847 desc
.range
.offset
= multi_pagebuffer
->offset
;
849 memcpy(desc
.range
.pfn_array
, multi_pagebuffer
->pfn_array
,
850 pfncount
* sizeof(u64
));
852 bufferlist
[0].iov_base
= &desc
;
853 bufferlist
[0].iov_len
= descsize
;
854 bufferlist
[1].iov_base
= buffer
;
855 bufferlist
[1].iov_len
= bufferlen
;
856 bufferlist
[2].iov_base
= &aligned_data
;
857 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
859 return hv_ringbuffer_write(channel
, bufferlist
, 3,
862 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer
);
865 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
866 * @channel: Pointer to vmbus_channel structure.
867 * @buffer: Pointer to the buffer you want to receive the data into.
868 * @bufferlen: Maximum size of what the the buffer will hold
869 * @buffer_actual_len: The actual size of the data after it was received
870 * @requestid: Identifier of the request
872 * Receives directly from the hyper-v vmbus and puts the data it received
873 * into Buffer. This will receive the data unparsed from hyper-v.
875 * Mainly used by Hyper-V drivers.
878 __vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
879 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
,
882 return hv_ringbuffer_read(channel
, buffer
, bufferlen
,
883 buffer_actual_len
, requestid
, raw
);
887 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
888 u32 bufferlen
, u32
*buffer_actual_len
,
891 return __vmbus_recvpacket(channel
, buffer
, bufferlen
,
892 buffer_actual_len
, requestid
, false);
894 EXPORT_SYMBOL(vmbus_recvpacket
);
897 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
899 int vmbus_recvpacket_raw(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
, true);
906 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);