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
->state
= channel
->state
;
73 memcpy(&debuginfo
->interfacetype
,
74 &channel
->offermsg
.offer
.if_type
, sizeof(uuid_le
));
75 memcpy(&debuginfo
->interface_instance
,
76 &channel
->offermsg
.offer
.if_instance
,
79 monitorpage
= (struct hv_monitor_page
*)vmbus_connection
.monitor_pages
;
81 debuginfo
->monitorid
= channel
->offermsg
.monitorid
;
83 debuginfo
->servermonitor_pending
=
84 monitorpage
->trigger_group
[monitor_group
].pending
;
85 debuginfo
->servermonitor_latency
=
86 monitorpage
->latency
[monitor_group
][monitor_offset
];
87 debuginfo
->servermonitor_connectionid
=
88 monitorpage
->parameter
[monitor_group
]
89 [monitor_offset
].connectionid
.u
.id
;
93 debuginfo
->clientmonitor_pending
=
94 monitorpage
->trigger_group
[monitor_group
].pending
;
95 debuginfo
->clientmonitor_latency
=
96 monitorpage
->latency
[monitor_group
][monitor_offset
];
97 debuginfo
->clientmonitor_connectionid
=
98 monitorpage
->parameter
[monitor_group
]
99 [monitor_offset
].connectionid
.u
.id
;
101 hv_ringbuffer_get_debuginfo(&channel
->inbound
, &debuginfo
->inbound
);
102 hv_ringbuffer_get_debuginfo(&channel
->outbound
, &debuginfo
->outbound
);
106 * vmbus_open - Open the specified channel.
108 int vmbus_open(struct vmbus_channel
*newchannel
, u32 send_ringbuffer_size
,
109 u32 recv_ringbuffer_size
, void *userdata
, u32 userdatalen
,
110 void (*onchannelcallback
)(void *context
), void *context
)
112 struct vmbus_channel_open_channel
*open_msg
;
113 struct vmbus_channel_msginfo
*open_info
= NULL
;
118 spin_lock_irqsave(&newchannel
->sc_lock
, flags
);
119 if (newchannel
->state
== CHANNEL_OPEN_STATE
) {
120 newchannel
->state
= CHANNEL_OPENING_STATE
;
122 spin_unlock_irqrestore(&newchannel
->sc_lock
, flags
);
125 spin_unlock_irqrestore(&newchannel
->sc_lock
, flags
);
127 newchannel
->onchannel_callback
= onchannelcallback
;
128 newchannel
->channel_callback_context
= context
;
130 /* Allocate the ring buffer */
131 out
= (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
132 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
138 in
= (void *)((unsigned long)out
+ send_ringbuffer_size
);
140 newchannel
->ringbuffer_pages
= out
;
141 newchannel
->ringbuffer_pagecount
= (send_ringbuffer_size
+
142 recv_ringbuffer_size
) >> PAGE_SHIFT
;
144 ret
= hv_ringbuffer_init(
145 &newchannel
->outbound
, out
, send_ringbuffer_size
);
152 ret
= hv_ringbuffer_init(
153 &newchannel
->inbound
, in
, recv_ringbuffer_size
);
160 /* Establish the gpadl for the ring buffer */
161 newchannel
->ringbuffer_gpadlhandle
= 0;
163 ret
= vmbus_establish_gpadl(newchannel
,
164 newchannel
->outbound
.ring_buffer
,
165 send_ringbuffer_size
+
166 recv_ringbuffer_size
,
167 &newchannel
->ringbuffer_gpadlhandle
);
174 /* Create and init the channel open message */
175 open_info
= kmalloc(sizeof(*open_info
) +
176 sizeof(struct vmbus_channel_open_channel
),
183 init_completion(&open_info
->waitevent
);
185 open_msg
= (struct vmbus_channel_open_channel
*)open_info
->msg
;
186 open_msg
->header
.msgtype
= CHANNELMSG_OPENCHANNEL
;
187 open_msg
->openid
= newchannel
->offermsg
.child_relid
;
188 open_msg
->child_relid
= newchannel
->offermsg
.child_relid
;
189 open_msg
->ringbuffer_gpadlhandle
= newchannel
->ringbuffer_gpadlhandle
;
190 open_msg
->downstream_ringbuffer_pageoffset
= send_ringbuffer_size
>>
192 open_msg
->target_vp
= newchannel
->target_vp
;
194 if (userdatalen
> MAX_USER_DEFINED_BYTES
) {
200 memcpy(open_msg
->userdata
, userdata
, userdatalen
);
202 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
203 list_add_tail(&open_info
->msglistentry
,
204 &vmbus_connection
.chn_msg_list
);
205 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
207 ret
= vmbus_post_msg(open_msg
,
208 sizeof(struct vmbus_channel_open_channel
));
213 t
= wait_for_completion_timeout(&open_info
->waitevent
, 5*HZ
);
220 if (open_info
->response
.open_result
.status
)
221 err
= open_info
->response
.open_result
.status
;
223 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
224 list_del(&open_info
->msglistentry
);
225 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
228 newchannel
->state
= CHANNEL_OPENED_STATE
;
234 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
235 list_del(&open_info
->msglistentry
);
236 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
239 free_pages((unsigned long)out
,
240 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
244 EXPORT_SYMBOL_GPL(vmbus_open
);
247 * create_gpadl_header - Creates a gpadl for the specified buffer
249 static int create_gpadl_header(void *kbuffer
, u32 size
,
250 struct vmbus_channel_msginfo
**msginfo
,
255 unsigned long long pfn
;
256 struct vmbus_channel_gpadl_header
*gpadl_header
;
257 struct vmbus_channel_gpadl_body
*gpadl_body
;
258 struct vmbus_channel_msginfo
*msgheader
;
259 struct vmbus_channel_msginfo
*msgbody
= NULL
;
262 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
264 pagecount
= size
>> PAGE_SHIFT
;
265 pfn
= virt_to_phys(kbuffer
) >> PAGE_SHIFT
;
267 /* do we need a gpadl body msg */
268 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
269 sizeof(struct vmbus_channel_gpadl_header
) -
270 sizeof(struct gpa_range
);
271 pfncount
= pfnsize
/ sizeof(u64
);
273 if (pagecount
> pfncount
) {
274 /* we need a gpadl body */
275 /* fill in the header */
276 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
277 sizeof(struct vmbus_channel_gpadl_header
) +
278 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
279 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
283 INIT_LIST_HEAD(&msgheader
->submsglist
);
284 msgheader
->msgsize
= msgsize
;
286 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
288 gpadl_header
->rangecount
= 1;
289 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
290 pagecount
* sizeof(u64
);
291 gpadl_header
->range
[0].byte_offset
= 0;
292 gpadl_header
->range
[0].byte_count
= size
;
293 for (i
= 0; i
< pfncount
; i
++)
294 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
295 *msginfo
= msgheader
;
299 pfnleft
= pagecount
- pfncount
;
301 /* how many pfns can we fit */
302 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
303 sizeof(struct vmbus_channel_gpadl_body
);
304 pfncount
= pfnsize
/ sizeof(u64
);
306 /* fill in the body */
308 if (pfnleft
> pfncount
)
313 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
314 sizeof(struct vmbus_channel_gpadl_body
) +
315 pfncurr
* sizeof(u64
);
316 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
319 struct vmbus_channel_msginfo
*pos
= NULL
;
320 struct vmbus_channel_msginfo
*tmp
= NULL
;
322 * Free up all the allocated messages.
324 list_for_each_entry_safe(pos
, tmp
,
325 &msgheader
->submsglist
,
328 list_del(&pos
->msglistentry
);
335 msgbody
->msgsize
= msgsize
;
338 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
341 * Gpadl is u32 and we are using a pointer which could
343 * This is governed by the guest/host protocol and
344 * so the hypervisor gurantees that this is ok.
346 for (i
= 0; i
< pfncurr
; i
++)
347 gpadl_body
->pfn
[i
] = pfn
+ pfnsum
+ i
;
349 /* add to msg header */
350 list_add_tail(&msgbody
->msglistentry
,
351 &msgheader
->submsglist
);
356 /* everything fits in a header */
357 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
358 sizeof(struct vmbus_channel_gpadl_header
) +
359 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
360 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
361 if (msgheader
== NULL
)
363 msgheader
->msgsize
= msgsize
;
365 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
367 gpadl_header
->rangecount
= 1;
368 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
369 pagecount
* sizeof(u64
);
370 gpadl_header
->range
[0].byte_offset
= 0;
371 gpadl_header
->range
[0].byte_count
= size
;
372 for (i
= 0; i
< pagecount
; i
++)
373 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
375 *msginfo
= msgheader
;
387 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
389 * @channel: a channel
390 * @kbuffer: from kmalloc
391 * @size: page-size multiple
392 * @gpadl_handle: some funky thing
394 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
395 u32 size
, u32
*gpadl_handle
)
397 struct vmbus_channel_gpadl_header
*gpadlmsg
;
398 struct vmbus_channel_gpadl_body
*gpadl_body
;
399 struct vmbus_channel_msginfo
*msginfo
= NULL
;
400 struct vmbus_channel_msginfo
*submsginfo
;
402 struct list_head
*curr
;
403 u32 next_gpadl_handle
;
408 next_gpadl_handle
= atomic_read(&vmbus_connection
.next_gpadl_handle
);
409 atomic_inc(&vmbus_connection
.next_gpadl_handle
);
411 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
, &msgcount
);
415 init_completion(&msginfo
->waitevent
);
417 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
418 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
419 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
420 gpadlmsg
->gpadl
= next_gpadl_handle
;
423 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
424 list_add_tail(&msginfo
->msglistentry
,
425 &vmbus_connection
.chn_msg_list
);
427 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
429 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
435 list_for_each(curr
, &msginfo
->submsglist
) {
437 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
439 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
441 gpadl_body
->header
.msgtype
=
442 CHANNELMSG_GPADL_BODY
;
443 gpadl_body
->gpadl
= next_gpadl_handle
;
445 ret
= vmbus_post_msg(gpadl_body
,
446 submsginfo
->msgsize
-
447 sizeof(*submsginfo
));
453 t
= wait_for_completion_timeout(&msginfo
->waitevent
, 5*HZ
);
457 /* At this point, we received the gpadl created msg */
458 *gpadl_handle
= gpadlmsg
->gpadl
;
461 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
462 list_del(&msginfo
->msglistentry
);
463 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
468 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
471 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
473 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
475 struct vmbus_channel_gpadl_teardown
*msg
;
476 struct vmbus_channel_msginfo
*info
;
480 info
= kmalloc(sizeof(*info
) +
481 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
485 init_completion(&info
->waitevent
);
487 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
489 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
490 msg
->child_relid
= channel
->offermsg
.child_relid
;
491 msg
->gpadl
= gpadl_handle
;
493 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
494 list_add_tail(&info
->msglistentry
,
495 &vmbus_connection
.chn_msg_list
);
496 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
497 ret
= vmbus_post_msg(msg
,
498 sizeof(struct vmbus_channel_gpadl_teardown
));
501 t
= wait_for_completion_timeout(&info
->waitevent
, 5*HZ
);
504 /* Received a torndown response */
505 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
506 list_del(&info
->msglistentry
);
507 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
512 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
514 static void vmbus_close_internal(struct vmbus_channel
*channel
)
516 struct vmbus_channel_close_channel
*msg
;
520 channel
->state
= CHANNEL_OPEN_STATE
;
521 channel
->sc_creation_callback
= NULL
;
522 /* Stop callback and cancel the timer asap */
523 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
524 channel
->onchannel_callback
= NULL
;
525 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
527 /* Send a closing message */
529 msg
= &channel
->close_msg
.msg
;
531 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
532 msg
->child_relid
= channel
->offermsg
.child_relid
;
534 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
));
537 /* Tear down the gpadl for the channel's ring buffer */
538 if (channel
->ringbuffer_gpadlhandle
)
539 vmbus_teardown_gpadl(channel
,
540 channel
->ringbuffer_gpadlhandle
);
542 /* Cleanup the ring buffers for this channel */
543 hv_ringbuffer_cleanup(&channel
->outbound
);
544 hv_ringbuffer_cleanup(&channel
->inbound
);
546 free_pages((unsigned long)channel
->ringbuffer_pages
,
547 get_order(channel
->ringbuffer_pagecount
* PAGE_SIZE
));
553 * vmbus_close - Close the specified channel
555 void vmbus_close(struct vmbus_channel
*channel
)
557 struct list_head
*cur
, *tmp
;
558 struct vmbus_channel
*cur_channel
;
560 if (channel
->primary_channel
!= NULL
) {
562 * We will only close sub-channels when
563 * the primary is closed.
568 * Close all the sub-channels first and then close the
571 list_for_each_safe(cur
, tmp
, &channel
->sc_list
) {
572 cur_channel
= list_entry(cur
, struct vmbus_channel
, sc_list
);
573 if (cur_channel
->state
!= CHANNEL_OPENED_STATE
)
575 vmbus_close_internal(cur_channel
);
578 * Now close the primary.
580 vmbus_close_internal(channel
);
582 EXPORT_SYMBOL_GPL(vmbus_close
);
585 * vmbus_sendpacket() - Send the specified buffer on the given channel
586 * @channel: Pointer to vmbus_channel structure.
587 * @buffer: Pointer to the buffer you want to receive the data into.
588 * @bufferlen: Maximum size of what the the buffer will hold
589 * @requestid: Identifier of the request
590 * @type: Type of packet that is being send e.g. negotiate, time
593 * Sends data in @buffer directly to hyper-v via the vmbus
594 * This will send the data unparsed to hyper-v.
596 * Mainly used by Hyper-V drivers.
598 int vmbus_sendpacket(struct vmbus_channel
*channel
, const void *buffer
,
599 u32 bufferlen
, u64 requestid
,
600 enum vmbus_packet_type type
, u32 flags
)
602 struct vmpacket_descriptor desc
;
603 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
604 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
605 struct scatterlist bufferlist
[3];
606 u64 aligned_data
= 0;
611 /* Setup the descriptor */
612 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
613 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
614 /* in 8-bytes granularity */
615 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
616 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
617 desc
.trans_id
= requestid
;
619 sg_init_table(bufferlist
, 3);
620 sg_set_buf(&bufferlist
[0], &desc
, sizeof(struct vmpacket_descriptor
));
621 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
622 sg_set_buf(&bufferlist
[2], &aligned_data
,
623 packetlen_aligned
- packetlen
);
625 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
627 if (ret
== 0 && signal
)
628 vmbus_setevent(channel
);
632 EXPORT_SYMBOL(vmbus_sendpacket
);
635 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
636 * packets using a GPADL Direct packet type.
638 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
639 struct hv_page_buffer pagebuffers
[],
640 u32 pagecount
, void *buffer
, u32 bufferlen
,
645 struct vmbus_channel_packet_page_buffer desc
;
648 u32 packetlen_aligned
;
649 struct scatterlist bufferlist
[3];
650 u64 aligned_data
= 0;
653 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
658 * Adjust the size down since vmbus_channel_packet_page_buffer is the
659 * largest size we support
661 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
662 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
663 sizeof(struct hv_page_buffer
));
664 packetlen
= descsize
+ bufferlen
;
665 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
667 /* Setup the descriptor */
668 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
669 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
670 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
671 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
672 desc
.transactionid
= requestid
;
673 desc
.rangecount
= pagecount
;
675 for (i
= 0; i
< pagecount
; i
++) {
676 desc
.range
[i
].len
= pagebuffers
[i
].len
;
677 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
678 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
681 sg_init_table(bufferlist
, 3);
682 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
683 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
684 sg_set_buf(&bufferlist
[2], &aligned_data
,
685 packetlen_aligned
- packetlen
);
687 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
689 if (ret
== 0 && signal
)
690 vmbus_setevent(channel
);
694 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
697 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
698 * using a GPADL Direct packet type.
700 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
701 struct hv_multipage_buffer
*multi_pagebuffer
,
702 void *buffer
, u32 bufferlen
, u64 requestid
)
705 struct vmbus_channel_packet_multipage_buffer desc
;
708 u32 packetlen_aligned
;
709 struct scatterlist bufferlist
[3];
710 u64 aligned_data
= 0;
712 u32 pfncount
= NUM_PAGES_SPANNED(multi_pagebuffer
->offset
,
713 multi_pagebuffer
->len
);
716 if ((pfncount
< 0) || (pfncount
> MAX_MULTIPAGE_BUFFER_COUNT
))
720 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
721 * the largest size we support
723 descsize
= sizeof(struct vmbus_channel_packet_multipage_buffer
) -
724 ((MAX_MULTIPAGE_BUFFER_COUNT
- pfncount
) *
726 packetlen
= descsize
+ bufferlen
;
727 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
730 /* Setup the descriptor */
731 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
732 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
733 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
734 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
735 desc
.transactionid
= requestid
;
738 desc
.range
.len
= multi_pagebuffer
->len
;
739 desc
.range
.offset
= multi_pagebuffer
->offset
;
741 memcpy(desc
.range
.pfn_array
, multi_pagebuffer
->pfn_array
,
742 pfncount
* sizeof(u64
));
744 sg_init_table(bufferlist
, 3);
745 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
746 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
747 sg_set_buf(&bufferlist
[2], &aligned_data
,
748 packetlen_aligned
- packetlen
);
750 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
752 if (ret
== 0 && signal
)
753 vmbus_setevent(channel
);
757 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer
);
760 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
761 * @channel: Pointer to vmbus_channel structure.
762 * @buffer: Pointer to the buffer you want to receive the data into.
763 * @bufferlen: Maximum size of what the the buffer will hold
764 * @buffer_actual_len: The actual size of the data after it was received
765 * @requestid: Identifier of the request
767 * Receives directly from the hyper-v vmbus and puts the data it received
768 * into Buffer. This will receive the data unparsed from hyper-v.
770 * Mainly used by Hyper-V drivers.
772 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
773 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
)
775 struct vmpacket_descriptor desc
;
781 *buffer_actual_len
= 0;
785 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
786 sizeof(struct vmpacket_descriptor
));
790 packetlen
= desc
.len8
<< 3;
791 userlen
= packetlen
- (desc
.offset8
<< 3);
793 *buffer_actual_len
= userlen
;
795 if (userlen
> bufferlen
) {
797 pr_err("Buffer too small - got %d needs %d\n",
802 *requestid
= desc
.trans_id
;
804 /* Copy over the packet to the user buffer */
805 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, userlen
,
806 (desc
.offset8
<< 3), &signal
);
809 vmbus_setevent(channel
);
813 EXPORT_SYMBOL(vmbus_recvpacket
);
816 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
818 int vmbus_recvpacket_raw(struct vmbus_channel
*channel
, void *buffer
,
819 u32 bufferlen
, u32
*buffer_actual_len
,
822 struct vmpacket_descriptor desc
;
827 *buffer_actual_len
= 0;
831 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
832 sizeof(struct vmpacket_descriptor
));
837 packetlen
= desc
.len8
<< 3;
839 *buffer_actual_len
= packetlen
;
841 if (packetlen
> bufferlen
) {
842 pr_err("Buffer too small - needed %d bytes but "
843 "got space for only %d bytes\n",
844 packetlen
, bufferlen
);
848 *requestid
= desc
.trans_id
;
850 /* Copy over the entire packet to the user buffer */
851 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, packetlen
, 0,
855 vmbus_setevent(channel
);
859 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);