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 static 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
);
70 * vmbus_open - Open the specified channel.
72 int vmbus_open(struct vmbus_channel
*newchannel
, u32 send_ringbuffer_size
,
73 u32 recv_ringbuffer_size
, void *userdata
, u32 userdatalen
,
74 void (*onchannelcallback
)(void *context
), void *context
)
76 struct vmbus_channel_open_channel
*open_msg
;
77 struct vmbus_channel_msginfo
*open_info
= NULL
;
82 if (send_ringbuffer_size
% PAGE_SIZE
||
83 recv_ringbuffer_size
% PAGE_SIZE
)
86 spin_lock_irqsave(&newchannel
->lock
, flags
);
87 if (newchannel
->state
== CHANNEL_OPEN_STATE
) {
88 newchannel
->state
= CHANNEL_OPENING_STATE
;
90 spin_unlock_irqrestore(&newchannel
->lock
, flags
);
93 spin_unlock_irqrestore(&newchannel
->lock
, flags
);
95 newchannel
->onchannel_callback
= onchannelcallback
;
96 newchannel
->channel_callback_context
= context
;
98 /* Allocate the ring buffer */
99 page
= alloc_pages_node(cpu_to_node(newchannel
->target_cpu
),
100 GFP_KERNEL
|__GFP_ZERO
,
101 get_order(send_ringbuffer_size
+
102 recv_ringbuffer_size
));
105 page
= alloc_pages(GFP_KERNEL
|__GFP_ZERO
,
106 get_order(send_ringbuffer_size
+
107 recv_ringbuffer_size
));
111 goto error_set_chnstate
;
114 newchannel
->ringbuffer_pages
= page_address(page
);
115 newchannel
->ringbuffer_pagecount
= (send_ringbuffer_size
+
116 recv_ringbuffer_size
) >> PAGE_SHIFT
;
118 ret
= hv_ringbuffer_init(&newchannel
->outbound
, page
,
119 send_ringbuffer_size
>> PAGE_SHIFT
);
123 goto error_free_pages
;
126 ret
= hv_ringbuffer_init(&newchannel
->inbound
,
127 &page
[send_ringbuffer_size
>> PAGE_SHIFT
],
128 recv_ringbuffer_size
>> PAGE_SHIFT
);
131 goto error_free_pages
;
135 /* Establish the gpadl for the ring buffer */
136 newchannel
->ringbuffer_gpadlhandle
= 0;
138 ret
= vmbus_establish_gpadl(newchannel
,
140 send_ringbuffer_size
+
141 recv_ringbuffer_size
,
142 &newchannel
->ringbuffer_gpadlhandle
);
146 goto error_free_pages
;
149 /* Create and init the channel open message */
150 open_info
= kmalloc(sizeof(*open_info
) +
151 sizeof(struct vmbus_channel_open_channel
),
155 goto error_free_gpadl
;
158 init_completion(&open_info
->waitevent
);
160 open_msg
= (struct vmbus_channel_open_channel
*)open_info
->msg
;
161 open_msg
->header
.msgtype
= CHANNELMSG_OPENCHANNEL
;
162 open_msg
->openid
= newchannel
->offermsg
.child_relid
;
163 open_msg
->child_relid
= newchannel
->offermsg
.child_relid
;
164 open_msg
->ringbuffer_gpadlhandle
= newchannel
->ringbuffer_gpadlhandle
;
165 open_msg
->downstream_ringbuffer_pageoffset
= send_ringbuffer_size
>>
167 open_msg
->target_vp
= newchannel
->target_vp
;
169 if (userdatalen
> MAX_USER_DEFINED_BYTES
) {
171 goto error_free_gpadl
;
175 memcpy(open_msg
->userdata
, userdata
, userdatalen
);
177 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
178 list_add_tail(&open_info
->msglistentry
,
179 &vmbus_connection
.chn_msg_list
);
180 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
182 ret
= vmbus_post_msg(open_msg
,
183 sizeof(struct vmbus_channel_open_channel
));
187 goto error_clean_msglist
;
190 wait_for_completion(&open_info
->waitevent
);
192 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
193 list_del(&open_info
->msglistentry
);
194 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
196 if (open_info
->response
.open_result
.status
) {
198 goto error_free_gpadl
;
201 newchannel
->state
= CHANNEL_OPENED_STATE
;
206 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
207 list_del(&open_info
->msglistentry
);
208 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
211 vmbus_teardown_gpadl(newchannel
, newchannel
->ringbuffer_gpadlhandle
);
214 hv_ringbuffer_cleanup(&newchannel
->outbound
);
215 hv_ringbuffer_cleanup(&newchannel
->inbound
);
217 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
219 newchannel
->state
= CHANNEL_OPEN_STATE
;
222 EXPORT_SYMBOL_GPL(vmbus_open
);
224 /* Used for Hyper-V Socket: a guest client's connect() to the host */
225 int vmbus_send_tl_connect_request(const uuid_le
*shv_guest_servie_id
,
226 const uuid_le
*shv_host_servie_id
)
228 struct vmbus_channel_tl_connect_request conn_msg
;
230 memset(&conn_msg
, 0, sizeof(conn_msg
));
231 conn_msg
.header
.msgtype
= CHANNELMSG_TL_CONNECT_REQUEST
;
232 conn_msg
.guest_endpoint_id
= *shv_guest_servie_id
;
233 conn_msg
.host_service_id
= *shv_host_servie_id
;
235 return vmbus_post_msg(&conn_msg
, sizeof(conn_msg
));
237 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request
);
240 * create_gpadl_header - Creates a gpadl for the specified buffer
242 static int create_gpadl_header(void *kbuffer
, u32 size
,
243 struct vmbus_channel_msginfo
**msginfo
)
247 struct vmbus_channel_gpadl_header
*gpadl_header
;
248 struct vmbus_channel_gpadl_body
*gpadl_body
;
249 struct vmbus_channel_msginfo
*msgheader
;
250 struct vmbus_channel_msginfo
*msgbody
= NULL
;
253 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
255 pagecount
= size
>> PAGE_SHIFT
;
257 /* do we need a gpadl body msg */
258 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
259 sizeof(struct vmbus_channel_gpadl_header
) -
260 sizeof(struct gpa_range
);
261 pfncount
= pfnsize
/ sizeof(u64
);
263 if (pagecount
> pfncount
) {
264 /* we need a gpadl body */
265 /* fill in the header */
266 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
267 sizeof(struct vmbus_channel_gpadl_header
) +
268 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
269 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
273 INIT_LIST_HEAD(&msgheader
->submsglist
);
274 msgheader
->msgsize
= msgsize
;
276 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
278 gpadl_header
->rangecount
= 1;
279 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
280 pagecount
* sizeof(u64
);
281 gpadl_header
->range
[0].byte_offset
= 0;
282 gpadl_header
->range
[0].byte_count
= size
;
283 for (i
= 0; i
< pfncount
; i
++)
284 gpadl_header
->range
[0].pfn_array
[i
] = slow_virt_to_phys(
285 kbuffer
+ PAGE_SIZE
* i
) >> PAGE_SHIFT
;
286 *msginfo
= msgheader
;
289 pfnleft
= pagecount
- pfncount
;
291 /* how many pfns can we fit */
292 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
293 sizeof(struct vmbus_channel_gpadl_body
);
294 pfncount
= pfnsize
/ sizeof(u64
);
296 /* fill in the body */
298 if (pfnleft
> pfncount
)
303 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
304 sizeof(struct vmbus_channel_gpadl_body
) +
305 pfncurr
* sizeof(u64
);
306 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
309 struct vmbus_channel_msginfo
*pos
= NULL
;
310 struct vmbus_channel_msginfo
*tmp
= NULL
;
312 * Free up all the allocated messages.
314 list_for_each_entry_safe(pos
, tmp
,
315 &msgheader
->submsglist
,
318 list_del(&pos
->msglistentry
);
325 msgbody
->msgsize
= msgsize
;
327 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
330 * Gpadl is u32 and we are using a pointer which could
332 * This is governed by the guest/host protocol and
333 * so the hypervisor gurantees that this is ok.
335 for (i
= 0; i
< pfncurr
; i
++)
336 gpadl_body
->pfn
[i
] = slow_virt_to_phys(
337 kbuffer
+ PAGE_SIZE
* (pfnsum
+ i
)) >>
340 /* add to msg header */
341 list_add_tail(&msgbody
->msglistentry
,
342 &msgheader
->submsglist
);
347 /* everything fits in a header */
348 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
349 sizeof(struct vmbus_channel_gpadl_header
) +
350 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
351 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
352 if (msgheader
== NULL
)
355 INIT_LIST_HEAD(&msgheader
->submsglist
);
356 msgheader
->msgsize
= msgsize
;
358 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
360 gpadl_header
->rangecount
= 1;
361 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
362 pagecount
* sizeof(u64
);
363 gpadl_header
->range
[0].byte_offset
= 0;
364 gpadl_header
->range
[0].byte_count
= size
;
365 for (i
= 0; i
< pagecount
; i
++)
366 gpadl_header
->range
[0].pfn_array
[i
] = slow_virt_to_phys(
367 kbuffer
+ PAGE_SIZE
* i
) >> PAGE_SHIFT
;
369 *msginfo
= msgheader
;
380 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
382 * @channel: a channel
383 * @kbuffer: from kmalloc or vmalloc
384 * @size: page-size multiple
385 * @gpadl_handle: some funky thing
387 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
388 u32 size
, u32
*gpadl_handle
)
390 struct vmbus_channel_gpadl_header
*gpadlmsg
;
391 struct vmbus_channel_gpadl_body
*gpadl_body
;
392 struct vmbus_channel_msginfo
*msginfo
= NULL
;
393 struct vmbus_channel_msginfo
*submsginfo
, *tmp
;
394 struct list_head
*curr
;
395 u32 next_gpadl_handle
;
400 (atomic_inc_return(&vmbus_connection
.next_gpadl_handle
) - 1);
402 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
);
406 init_completion(&msginfo
->waitevent
);
408 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
409 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
410 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
411 gpadlmsg
->gpadl
= next_gpadl_handle
;
414 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
415 list_add_tail(&msginfo
->msglistentry
,
416 &vmbus_connection
.chn_msg_list
);
418 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
420 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
425 list_for_each(curr
, &msginfo
->submsglist
) {
426 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
428 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
430 gpadl_body
->header
.msgtype
=
431 CHANNELMSG_GPADL_BODY
;
432 gpadl_body
->gpadl
= next_gpadl_handle
;
434 ret
= vmbus_post_msg(gpadl_body
,
435 submsginfo
->msgsize
-
436 sizeof(*submsginfo
));
441 wait_for_completion(&msginfo
->waitevent
);
443 /* At this point, we received the gpadl created msg */
444 *gpadl_handle
= gpadlmsg
->gpadl
;
447 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
448 list_del(&msginfo
->msglistentry
);
449 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
450 list_for_each_entry_safe(submsginfo
, tmp
, &msginfo
->submsglist
,
458 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
461 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
463 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
465 struct vmbus_channel_gpadl_teardown
*msg
;
466 struct vmbus_channel_msginfo
*info
;
470 info
= kmalloc(sizeof(*info
) +
471 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
475 init_completion(&info
->waitevent
);
477 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
479 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
480 msg
->child_relid
= channel
->offermsg
.child_relid
;
481 msg
->gpadl
= gpadl_handle
;
483 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
484 list_add_tail(&info
->msglistentry
,
485 &vmbus_connection
.chn_msg_list
);
486 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
487 ret
= vmbus_post_msg(msg
,
488 sizeof(struct vmbus_channel_gpadl_teardown
));
493 wait_for_completion(&info
->waitevent
);
496 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
497 list_del(&info
->msglistentry
);
498 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
503 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
505 static void reset_channel_cb(void *arg
)
507 struct vmbus_channel
*channel
= arg
;
509 channel
->onchannel_callback
= NULL
;
512 static int vmbus_close_internal(struct vmbus_channel
*channel
)
514 struct vmbus_channel_close_channel
*msg
;
518 * process_chn_event(), running in the tasklet, can race
519 * with vmbus_close_internal() in the case of SMP guest, e.g., when
520 * the former is accessing channel->inbound.ring_buffer, the latter
521 * could be freeing the ring_buffer pages.
523 * To resolve the race, we can serialize them by disabling the
524 * tasklet when the latter is running here.
526 hv_event_tasklet_disable(channel
);
529 * In case a device driver's probe() fails (e.g.,
530 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
531 * rescinded later (e.g., we dynamically disble an Integrated Service
532 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
533 * here we should skip most of the below cleanup work.
535 if (channel
->state
!= CHANNEL_OPENED_STATE
) {
540 channel
->state
= CHANNEL_OPEN_STATE
;
541 channel
->sc_creation_callback
= NULL
;
542 /* Stop callback and cancel the timer asap */
543 if (channel
->target_cpu
!= get_cpu()) {
545 smp_call_function_single(channel
->target_cpu
, reset_channel_cb
,
548 reset_channel_cb(channel
);
552 /* Send a closing message */
554 msg
= &channel
->close_msg
.msg
;
556 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
557 msg
->child_relid
= channel
->offermsg
.child_relid
;
559 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
));
562 pr_err("Close failed: close post msg return is %d\n", ret
);
564 * If we failed to post the close msg,
565 * it is perhaps better to leak memory.
570 /* Tear down the gpadl for the channel's ring buffer */
571 if (channel
->ringbuffer_gpadlhandle
) {
572 ret
= vmbus_teardown_gpadl(channel
,
573 channel
->ringbuffer_gpadlhandle
);
575 pr_err("Close failed: teardown gpadl return %d\n", ret
);
577 * If we failed to teardown gpadl,
578 * it is perhaps better to leak memory.
584 /* Cleanup the ring buffers for this channel */
585 hv_ringbuffer_cleanup(&channel
->outbound
);
586 hv_ringbuffer_cleanup(&channel
->inbound
);
588 free_pages((unsigned long)channel
->ringbuffer_pages
,
589 get_order(channel
->ringbuffer_pagecount
* PAGE_SIZE
));
592 hv_event_tasklet_enable(channel
);
598 * vmbus_close - Close the specified channel
600 void vmbus_close(struct vmbus_channel
*channel
)
602 struct list_head
*cur
, *tmp
;
603 struct vmbus_channel
*cur_channel
;
605 if (channel
->primary_channel
!= NULL
) {
607 * We will only close sub-channels when
608 * the primary is closed.
613 * Close all the sub-channels first and then close the
616 list_for_each_safe(cur
, tmp
, &channel
->sc_list
) {
617 cur_channel
= list_entry(cur
, struct vmbus_channel
, sc_list
);
618 if (cur_channel
->state
!= CHANNEL_OPENED_STATE
)
620 vmbus_close_internal(cur_channel
);
623 * Now close the primary.
625 vmbus_close_internal(channel
);
627 EXPORT_SYMBOL_GPL(vmbus_close
);
629 int vmbus_sendpacket_ctl(struct vmbus_channel
*channel
, void *buffer
,
630 u32 bufferlen
, u64 requestid
,
631 enum vmbus_packet_type type
, u32 flags
, bool kick_q
)
633 struct vmpacket_descriptor desc
;
634 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
635 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
636 struct kvec bufferlist
[3];
637 u64 aligned_data
= 0;
640 bool lock
= channel
->acquire_ring_lock
;
641 int num_vecs
= ((bufferlen
!= 0) ? 3 : 1);
644 /* Setup the descriptor */
645 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
646 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
647 /* in 8-bytes granularity */
648 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
649 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
650 desc
.trans_id
= requestid
;
652 bufferlist
[0].iov_base
= &desc
;
653 bufferlist
[0].iov_len
= sizeof(struct vmpacket_descriptor
);
654 bufferlist
[1].iov_base
= buffer
;
655 bufferlist
[1].iov_len
= bufferlen
;
656 bufferlist
[2].iov_base
= &aligned_data
;
657 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
659 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, num_vecs
,
660 &signal
, lock
, channel
->signal_policy
);
663 * Signalling the host is conditional on many factors:
664 * 1. The ring state changed from being empty to non-empty.
665 * This is tracked by the variable "signal".
666 * 2. The variable kick_q tracks if more data will be placed
667 * on the ring. We will not signal if more data is
670 * Based on the channel signal state, we will decide
671 * which signaling policy will be applied.
673 * If we cannot write to the ring-buffer; signal the host
674 * even if we may not have written anything. This is a rare
675 * enough condition that it should not matter.
676 * NOTE: in this case, the hvsock channel is an exception, because
677 * it looks the host side's hvsock implementation has a throttling
678 * mechanism which can hurt the performance otherwise.
681 if (((ret
== 0) && kick_q
&& signal
) ||
682 (ret
&& !is_hvsock_channel(channel
)))
683 vmbus_setevent(channel
);
687 EXPORT_SYMBOL(vmbus_sendpacket_ctl
);
690 * vmbus_sendpacket() - Send the specified buffer on the given channel
691 * @channel: Pointer to vmbus_channel structure.
692 * @buffer: Pointer to the buffer you want to receive the data into.
693 * @bufferlen: Maximum size of what the the buffer will hold
694 * @requestid: Identifier of the request
695 * @type: Type of packet that is being send e.g. negotiate, time
698 * Sends data in @buffer directly to hyper-v via the vmbus
699 * This will send the data unparsed to hyper-v.
701 * Mainly used by Hyper-V drivers.
703 int vmbus_sendpacket(struct vmbus_channel
*channel
, void *buffer
,
704 u32 bufferlen
, u64 requestid
,
705 enum vmbus_packet_type type
, u32 flags
)
707 return vmbus_sendpacket_ctl(channel
, buffer
, bufferlen
, requestid
,
710 EXPORT_SYMBOL(vmbus_sendpacket
);
713 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
714 * packets using a GPADL Direct packet type. This interface allows you
715 * to control notifying the host. This will be useful for sending
716 * batched data. Also the sender can control the send flags
719 int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel
*channel
,
720 struct hv_page_buffer pagebuffers
[],
721 u32 pagecount
, void *buffer
, u32 bufferlen
,
728 struct vmbus_channel_packet_page_buffer desc
;
731 u32 packetlen_aligned
;
732 struct kvec bufferlist
[3];
733 u64 aligned_data
= 0;
735 bool lock
= channel
->acquire_ring_lock
;
737 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
742 * Adjust the size down since vmbus_channel_packet_page_buffer is the
743 * largest size we support
745 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
746 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
747 sizeof(struct hv_page_buffer
));
748 packetlen
= descsize
+ bufferlen
;
749 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
751 /* Setup the descriptor */
752 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
754 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
755 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
756 desc
.transactionid
= requestid
;
757 desc
.rangecount
= pagecount
;
759 for (i
= 0; i
< pagecount
; i
++) {
760 desc
.range
[i
].len
= pagebuffers
[i
].len
;
761 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
762 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
765 bufferlist
[0].iov_base
= &desc
;
766 bufferlist
[0].iov_len
= descsize
;
767 bufferlist
[1].iov_base
= buffer
;
768 bufferlist
[1].iov_len
= bufferlen
;
769 bufferlist
[2].iov_base
= &aligned_data
;
770 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
772 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3,
773 &signal
, lock
, channel
->signal_policy
);
776 * Signalling the host is conditional on many factors:
777 * 1. The ring state changed from being empty to non-empty.
778 * This is tracked by the variable "signal".
779 * 2. The variable kick_q tracks if more data will be placed
780 * on the ring. We will not signal if more data is
783 * Based on the channel signal state, we will decide
784 * which signaling policy will be applied.
786 * If we cannot write to the ring-buffer; signal the host
787 * even if we may not have written anything. This is a rare
788 * enough condition that it should not matter.
791 if (((ret
== 0) && kick_q
&& signal
) || (ret
))
792 vmbus_setevent(channel
);
796 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl
);
799 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
800 * packets using a GPADL Direct packet type.
802 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
803 struct hv_page_buffer pagebuffers
[],
804 u32 pagecount
, void *buffer
, u32 bufferlen
,
807 u32 flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
808 return vmbus_sendpacket_pagebuffer_ctl(channel
, pagebuffers
, pagecount
,
809 buffer
, bufferlen
, requestid
,
813 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
816 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
817 * using a GPADL Direct packet type.
818 * The buffer includes the vmbus descriptor.
820 int vmbus_sendpacket_mpb_desc(struct vmbus_channel
*channel
,
821 struct vmbus_packet_mpb_array
*desc
,
823 void *buffer
, u32 bufferlen
, u64 requestid
)
827 u32 packetlen_aligned
;
828 struct kvec bufferlist
[3];
829 u64 aligned_data
= 0;
831 bool lock
= channel
->acquire_ring_lock
;
833 packetlen
= desc_size
+ bufferlen
;
834 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
836 /* Setup the descriptor */
837 desc
->type
= VM_PKT_DATA_USING_GPA_DIRECT
;
838 desc
->flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
839 desc
->dataoffset8
= desc_size
>> 3; /* in 8-bytes grandularity */
840 desc
->length8
= (u16
)(packetlen_aligned
>> 3);
841 desc
->transactionid
= requestid
;
842 desc
->rangecount
= 1;
844 bufferlist
[0].iov_base
= desc
;
845 bufferlist
[0].iov_len
= desc_size
;
846 bufferlist
[1].iov_base
= buffer
;
847 bufferlist
[1].iov_len
= bufferlen
;
848 bufferlist
[2].iov_base
= &aligned_data
;
849 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
851 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3,
852 &signal
, lock
, channel
->signal_policy
);
854 if (ret
== 0 && signal
)
855 vmbus_setevent(channel
);
859 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc
);
862 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
863 * using a GPADL Direct packet type.
865 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
866 struct hv_multipage_buffer
*multi_pagebuffer
,
867 void *buffer
, u32 bufferlen
, u64 requestid
)
870 struct vmbus_channel_packet_multipage_buffer desc
;
873 u32 packetlen_aligned
;
874 struct kvec bufferlist
[3];
875 u64 aligned_data
= 0;
877 bool lock
= channel
->acquire_ring_lock
;
878 u32 pfncount
= NUM_PAGES_SPANNED(multi_pagebuffer
->offset
,
879 multi_pagebuffer
->len
);
881 if (pfncount
> MAX_MULTIPAGE_BUFFER_COUNT
)
885 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
886 * the largest size we support
888 descsize
= sizeof(struct vmbus_channel_packet_multipage_buffer
) -
889 ((MAX_MULTIPAGE_BUFFER_COUNT
- pfncount
) *
891 packetlen
= descsize
+ bufferlen
;
892 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
895 /* Setup the descriptor */
896 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
897 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
898 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
899 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
900 desc
.transactionid
= requestid
;
903 desc
.range
.len
= multi_pagebuffer
->len
;
904 desc
.range
.offset
= multi_pagebuffer
->offset
;
906 memcpy(desc
.range
.pfn_array
, multi_pagebuffer
->pfn_array
,
907 pfncount
* sizeof(u64
));
909 bufferlist
[0].iov_base
= &desc
;
910 bufferlist
[0].iov_len
= descsize
;
911 bufferlist
[1].iov_base
= buffer
;
912 bufferlist
[1].iov_len
= bufferlen
;
913 bufferlist
[2].iov_base
= &aligned_data
;
914 bufferlist
[2].iov_len
= (packetlen_aligned
- packetlen
);
916 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3,
917 &signal
, lock
, channel
->signal_policy
);
919 if (ret
== 0 && signal
)
920 vmbus_setevent(channel
);
924 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer
);
927 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
928 * @channel: Pointer to vmbus_channel structure.
929 * @buffer: Pointer to the buffer you want to receive the data into.
930 * @bufferlen: Maximum size of what the the buffer will hold
931 * @buffer_actual_len: The actual size of the data after it was received
932 * @requestid: Identifier of the request
934 * Receives directly from the hyper-v vmbus and puts the data it received
935 * into Buffer. This will receive the data unparsed from hyper-v.
937 * Mainly used by Hyper-V drivers.
940 __vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
941 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
,
947 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, bufferlen
,
948 buffer_actual_len
, requestid
, &signal
, raw
);
951 vmbus_setevent(channel
);
956 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
957 u32 bufferlen
, u32
*buffer_actual_len
,
960 return __vmbus_recvpacket(channel
, buffer
, bufferlen
,
961 buffer_actual_len
, requestid
, false);
963 EXPORT_SYMBOL(vmbus_recvpacket
);
966 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
968 int vmbus_recvpacket_raw(struct vmbus_channel
*channel
, void *buffer
,
969 u32 bufferlen
, u32
*buffer_actual_len
,
972 return __vmbus_recvpacket(channel
, buffer
, bufferlen
,
973 buffer_actual_len
, requestid
, true);
975 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);