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 memcpy(&debuginfo
->interfacetype
,
73 &channel
->offermsg
.offer
.if_type
, sizeof(uuid_le
));
74 memcpy(&debuginfo
->interface_instance
,
75 &channel
->offermsg
.offer
.if_instance
,
78 monitorpage
= (struct hv_monitor_page
*)vmbus_connection
.monitor_pages
;
80 debuginfo
->monitorid
= channel
->offermsg
.monitorid
;
82 debuginfo
->servermonitor_pending
=
83 monitorpage
->trigger_group
[monitor_group
].pending
;
84 debuginfo
->servermonitor_latency
=
85 monitorpage
->latency
[monitor_group
][monitor_offset
];
86 debuginfo
->servermonitor_connectionid
=
87 monitorpage
->parameter
[monitor_group
]
88 [monitor_offset
].connectionid
.u
.id
;
92 debuginfo
->clientmonitor_pending
=
93 monitorpage
->trigger_group
[monitor_group
].pending
;
94 debuginfo
->clientmonitor_latency
=
95 monitorpage
->latency
[monitor_group
][monitor_offset
];
96 debuginfo
->clientmonitor_connectionid
=
97 monitorpage
->parameter
[monitor_group
]
98 [monitor_offset
].connectionid
.u
.id
;
100 hv_ringbuffer_get_debuginfo(&channel
->inbound
, &debuginfo
->inbound
);
101 hv_ringbuffer_get_debuginfo(&channel
->outbound
, &debuginfo
->outbound
);
105 * vmbus_open - Open the specified channel.
107 int vmbus_open(struct vmbus_channel
*newchannel
, u32 send_ringbuffer_size
,
108 u32 recv_ringbuffer_size
, void *userdata
, u32 userdatalen
,
109 void (*onchannelcallback
)(void *context
), void *context
)
111 struct vmbus_channel_open_channel
*open_msg
;
112 struct vmbus_channel_msginfo
*open_info
= NULL
;
117 spin_lock_irqsave(&newchannel
->sc_lock
, flags
);
118 if (newchannel
->state
== CHANNEL_OPEN_STATE
) {
119 newchannel
->state
= CHANNEL_OPENING_STATE
;
121 spin_unlock_irqrestore(&newchannel
->sc_lock
, flags
);
124 spin_unlock_irqrestore(&newchannel
->sc_lock
, flags
);
126 newchannel
->onchannel_callback
= onchannelcallback
;
127 newchannel
->channel_callback_context
= context
;
129 /* Allocate the ring buffer */
130 out
= (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
131 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
137 in
= (void *)((unsigned long)out
+ send_ringbuffer_size
);
139 newchannel
->ringbuffer_pages
= out
;
140 newchannel
->ringbuffer_pagecount
= (send_ringbuffer_size
+
141 recv_ringbuffer_size
) >> PAGE_SHIFT
;
143 ret
= hv_ringbuffer_init(
144 &newchannel
->outbound
, out
, send_ringbuffer_size
);
151 ret
= hv_ringbuffer_init(
152 &newchannel
->inbound
, in
, recv_ringbuffer_size
);
159 /* Establish the gpadl for the ring buffer */
160 newchannel
->ringbuffer_gpadlhandle
= 0;
162 ret
= vmbus_establish_gpadl(newchannel
,
163 newchannel
->outbound
.ring_buffer
,
164 send_ringbuffer_size
+
165 recv_ringbuffer_size
,
166 &newchannel
->ringbuffer_gpadlhandle
);
173 /* Create and init the channel open message */
174 open_info
= kmalloc(sizeof(*open_info
) +
175 sizeof(struct vmbus_channel_open_channel
),
182 init_completion(&open_info
->waitevent
);
184 open_msg
= (struct vmbus_channel_open_channel
*)open_info
->msg
;
185 open_msg
->header
.msgtype
= CHANNELMSG_OPENCHANNEL
;
186 open_msg
->openid
= newchannel
->offermsg
.child_relid
;
187 open_msg
->child_relid
= newchannel
->offermsg
.child_relid
;
188 open_msg
->ringbuffer_gpadlhandle
= newchannel
->ringbuffer_gpadlhandle
;
189 open_msg
->downstream_ringbuffer_pageoffset
= send_ringbuffer_size
>>
191 open_msg
->target_vp
= newchannel
->target_vp
;
193 if (userdatalen
> MAX_USER_DEFINED_BYTES
) {
199 memcpy(open_msg
->userdata
, userdata
, userdatalen
);
201 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
202 list_add_tail(&open_info
->msglistentry
,
203 &vmbus_connection
.chn_msg_list
);
204 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
206 ret
= vmbus_post_msg(open_msg
,
207 sizeof(struct vmbus_channel_open_channel
));
212 t
= wait_for_completion_timeout(&open_info
->waitevent
, 5*HZ
);
219 if (open_info
->response
.open_result
.status
)
220 err
= open_info
->response
.open_result
.status
;
222 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
223 list_del(&open_info
->msglistentry
);
224 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
227 newchannel
->state
= CHANNEL_OPENED_STATE
;
233 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
234 list_del(&open_info
->msglistentry
);
235 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
238 free_pages((unsigned long)out
,
239 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
243 EXPORT_SYMBOL_GPL(vmbus_open
);
246 * create_gpadl_header - Creates a gpadl for the specified buffer
248 static int create_gpadl_header(void *kbuffer
, u32 size
,
249 struct vmbus_channel_msginfo
**msginfo
,
254 unsigned long long pfn
;
255 struct vmbus_channel_gpadl_header
*gpadl_header
;
256 struct vmbus_channel_gpadl_body
*gpadl_body
;
257 struct vmbus_channel_msginfo
*msgheader
;
258 struct vmbus_channel_msginfo
*msgbody
= NULL
;
261 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
263 pagecount
= size
>> PAGE_SHIFT
;
264 pfn
= virt_to_phys(kbuffer
) >> PAGE_SHIFT
;
266 /* do we need a gpadl body msg */
267 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
268 sizeof(struct vmbus_channel_gpadl_header
) -
269 sizeof(struct gpa_range
);
270 pfncount
= pfnsize
/ sizeof(u64
);
272 if (pagecount
> pfncount
) {
273 /* we need a gpadl body */
274 /* fill in the header */
275 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
276 sizeof(struct vmbus_channel_gpadl_header
) +
277 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
278 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
282 INIT_LIST_HEAD(&msgheader
->submsglist
);
283 msgheader
->msgsize
= msgsize
;
285 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
287 gpadl_header
->rangecount
= 1;
288 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
289 pagecount
* sizeof(u64
);
290 gpadl_header
->range
[0].byte_offset
= 0;
291 gpadl_header
->range
[0].byte_count
= size
;
292 for (i
= 0; i
< pfncount
; i
++)
293 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
294 *msginfo
= msgheader
;
298 pfnleft
= pagecount
- pfncount
;
300 /* how many pfns can we fit */
301 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
302 sizeof(struct vmbus_channel_gpadl_body
);
303 pfncount
= pfnsize
/ sizeof(u64
);
305 /* fill in the body */
307 if (pfnleft
> pfncount
)
312 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
313 sizeof(struct vmbus_channel_gpadl_body
) +
314 pfncurr
* sizeof(u64
);
315 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
318 struct vmbus_channel_msginfo
*pos
= NULL
;
319 struct vmbus_channel_msginfo
*tmp
= NULL
;
321 * Free up all the allocated messages.
323 list_for_each_entry_safe(pos
, tmp
,
324 &msgheader
->submsglist
,
327 list_del(&pos
->msglistentry
);
334 msgbody
->msgsize
= msgsize
;
337 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
340 * Gpadl is u32 and we are using a pointer which could
342 * This is governed by the guest/host protocol and
343 * so the hypervisor gurantees that this is ok.
345 for (i
= 0; i
< pfncurr
; i
++)
346 gpadl_body
->pfn
[i
] = pfn
+ pfnsum
+ i
;
348 /* add to msg header */
349 list_add_tail(&msgbody
->msglistentry
,
350 &msgheader
->submsglist
);
355 /* everything fits in a header */
356 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
357 sizeof(struct vmbus_channel_gpadl_header
) +
358 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
359 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
360 if (msgheader
== NULL
)
362 msgheader
->msgsize
= msgsize
;
364 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
366 gpadl_header
->rangecount
= 1;
367 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
368 pagecount
* sizeof(u64
);
369 gpadl_header
->range
[0].byte_offset
= 0;
370 gpadl_header
->range
[0].byte_count
= size
;
371 for (i
= 0; i
< pagecount
; i
++)
372 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
374 *msginfo
= msgheader
;
386 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
388 * @channel: a channel
389 * @kbuffer: from kmalloc
390 * @size: page-size multiple
391 * @gpadl_handle: some funky thing
393 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
394 u32 size
, u32
*gpadl_handle
)
396 struct vmbus_channel_gpadl_header
*gpadlmsg
;
397 struct vmbus_channel_gpadl_body
*gpadl_body
;
398 struct vmbus_channel_msginfo
*msginfo
= NULL
;
399 struct vmbus_channel_msginfo
*submsginfo
;
401 struct list_head
*curr
;
402 u32 next_gpadl_handle
;
407 next_gpadl_handle
= atomic_read(&vmbus_connection
.next_gpadl_handle
);
408 atomic_inc(&vmbus_connection
.next_gpadl_handle
);
410 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
, &msgcount
);
414 init_completion(&msginfo
->waitevent
);
416 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
417 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
418 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
419 gpadlmsg
->gpadl
= next_gpadl_handle
;
422 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
423 list_add_tail(&msginfo
->msglistentry
,
424 &vmbus_connection
.chn_msg_list
);
426 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
428 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
434 list_for_each(curr
, &msginfo
->submsglist
) {
436 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
438 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
440 gpadl_body
->header
.msgtype
=
441 CHANNELMSG_GPADL_BODY
;
442 gpadl_body
->gpadl
= next_gpadl_handle
;
444 ret
= vmbus_post_msg(gpadl_body
,
445 submsginfo
->msgsize
-
446 sizeof(*submsginfo
));
452 t
= wait_for_completion_timeout(&msginfo
->waitevent
, 5*HZ
);
456 /* At this point, we received the gpadl created msg */
457 *gpadl_handle
= gpadlmsg
->gpadl
;
460 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
461 list_del(&msginfo
->msglistentry
);
462 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
467 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
470 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
472 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
474 struct vmbus_channel_gpadl_teardown
*msg
;
475 struct vmbus_channel_msginfo
*info
;
479 info
= kmalloc(sizeof(*info
) +
480 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
484 init_completion(&info
->waitevent
);
486 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
488 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
489 msg
->child_relid
= channel
->offermsg
.child_relid
;
490 msg
->gpadl
= gpadl_handle
;
492 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
493 list_add_tail(&info
->msglistentry
,
494 &vmbus_connection
.chn_msg_list
);
495 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
496 ret
= vmbus_post_msg(msg
,
497 sizeof(struct vmbus_channel_gpadl_teardown
));
500 t
= wait_for_completion_timeout(&info
->waitevent
, 5*HZ
);
503 /* Received a torndown response */
504 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
505 list_del(&info
->msglistentry
);
506 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
511 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
513 static void vmbus_close_internal(struct vmbus_channel
*channel
)
515 struct vmbus_channel_close_channel
*msg
;
519 channel
->state
= CHANNEL_OPEN_STATE
;
520 channel
->sc_creation_callback
= NULL
;
521 /* Stop callback and cancel the timer asap */
522 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
523 channel
->onchannel_callback
= NULL
;
524 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
526 /* Send a closing message */
528 msg
= &channel
->close_msg
.msg
;
530 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
531 msg
->child_relid
= channel
->offermsg
.child_relid
;
533 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
));
536 /* Tear down the gpadl for the channel's ring buffer */
537 if (channel
->ringbuffer_gpadlhandle
)
538 vmbus_teardown_gpadl(channel
,
539 channel
->ringbuffer_gpadlhandle
);
541 /* Cleanup the ring buffers for this channel */
542 hv_ringbuffer_cleanup(&channel
->outbound
);
543 hv_ringbuffer_cleanup(&channel
->inbound
);
545 free_pages((unsigned long)channel
->ringbuffer_pages
,
546 get_order(channel
->ringbuffer_pagecount
* PAGE_SIZE
));
552 * vmbus_close - Close the specified channel
554 void vmbus_close(struct vmbus_channel
*channel
)
556 struct list_head
*cur
, *tmp
;
557 struct vmbus_channel
*cur_channel
;
559 if (channel
->primary_channel
!= NULL
) {
561 * We will only close sub-channels when
562 * the primary is closed.
567 * Close all the sub-channels first and then close the
570 list_for_each_safe(cur
, tmp
, &channel
->sc_list
) {
571 cur_channel
= list_entry(cur
, struct vmbus_channel
, sc_list
);
572 if (cur_channel
->state
!= CHANNEL_OPENED_STATE
)
574 vmbus_close_internal(cur_channel
);
577 * Now close the primary.
579 vmbus_close_internal(channel
);
581 EXPORT_SYMBOL_GPL(vmbus_close
);
584 * vmbus_sendpacket() - Send the specified buffer on the given channel
585 * @channel: Pointer to vmbus_channel structure.
586 * @buffer: Pointer to the buffer you want to receive the data into.
587 * @bufferlen: Maximum size of what the the buffer will hold
588 * @requestid: Identifier of the request
589 * @type: Type of packet that is being send e.g. negotiate, time
592 * Sends data in @buffer directly to hyper-v via the vmbus
593 * This will send the data unparsed to hyper-v.
595 * Mainly used by Hyper-V drivers.
597 int vmbus_sendpacket(struct vmbus_channel
*channel
, const void *buffer
,
598 u32 bufferlen
, u64 requestid
,
599 enum vmbus_packet_type type
, u32 flags
)
601 struct vmpacket_descriptor desc
;
602 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
603 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
604 struct scatterlist bufferlist
[3];
605 u64 aligned_data
= 0;
610 /* Setup the descriptor */
611 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
612 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
613 /* in 8-bytes granularity */
614 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
615 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
616 desc
.trans_id
= requestid
;
618 sg_init_table(bufferlist
, 3);
619 sg_set_buf(&bufferlist
[0], &desc
, sizeof(struct vmpacket_descriptor
));
620 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
621 sg_set_buf(&bufferlist
[2], &aligned_data
,
622 packetlen_aligned
- packetlen
);
624 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
626 if (ret
== 0 && signal
)
627 vmbus_setevent(channel
);
631 EXPORT_SYMBOL(vmbus_sendpacket
);
634 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
635 * packets using a GPADL Direct packet type.
637 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
638 struct hv_page_buffer pagebuffers
[],
639 u32 pagecount
, void *buffer
, u32 bufferlen
,
644 struct vmbus_channel_packet_page_buffer desc
;
647 u32 packetlen_aligned
;
648 struct scatterlist bufferlist
[3];
649 u64 aligned_data
= 0;
652 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
657 * Adjust the size down since vmbus_channel_packet_page_buffer is the
658 * largest size we support
660 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
661 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
662 sizeof(struct hv_page_buffer
));
663 packetlen
= descsize
+ bufferlen
;
664 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
666 /* Setup the descriptor */
667 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
668 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
669 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
670 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
671 desc
.transactionid
= requestid
;
672 desc
.rangecount
= pagecount
;
674 for (i
= 0; i
< pagecount
; i
++) {
675 desc
.range
[i
].len
= pagebuffers
[i
].len
;
676 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
677 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
680 sg_init_table(bufferlist
, 3);
681 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
682 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
683 sg_set_buf(&bufferlist
[2], &aligned_data
,
684 packetlen_aligned
- packetlen
);
686 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
688 if (ret
== 0 && signal
)
689 vmbus_setevent(channel
);
693 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
696 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
697 * using a GPADL Direct packet type.
699 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
700 struct hv_multipage_buffer
*multi_pagebuffer
,
701 void *buffer
, u32 bufferlen
, u64 requestid
)
704 struct vmbus_channel_packet_multipage_buffer desc
;
707 u32 packetlen_aligned
;
708 struct scatterlist bufferlist
[3];
709 u64 aligned_data
= 0;
711 u32 pfncount
= NUM_PAGES_SPANNED(multi_pagebuffer
->offset
,
712 multi_pagebuffer
->len
);
715 if ((pfncount
< 0) || (pfncount
> MAX_MULTIPAGE_BUFFER_COUNT
))
719 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
720 * the largest size we support
722 descsize
= sizeof(struct vmbus_channel_packet_multipage_buffer
) -
723 ((MAX_MULTIPAGE_BUFFER_COUNT
- pfncount
) *
725 packetlen
= descsize
+ bufferlen
;
726 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
729 /* Setup the descriptor */
730 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
731 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
732 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
733 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
734 desc
.transactionid
= requestid
;
737 desc
.range
.len
= multi_pagebuffer
->len
;
738 desc
.range
.offset
= multi_pagebuffer
->offset
;
740 memcpy(desc
.range
.pfn_array
, multi_pagebuffer
->pfn_array
,
741 pfncount
* sizeof(u64
));
743 sg_init_table(bufferlist
, 3);
744 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
745 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
746 sg_set_buf(&bufferlist
[2], &aligned_data
,
747 packetlen_aligned
- packetlen
);
749 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3, &signal
);
751 if (ret
== 0 && signal
)
752 vmbus_setevent(channel
);
756 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer
);
759 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
760 * @channel: Pointer to vmbus_channel structure.
761 * @buffer: Pointer to the buffer you want to receive the data into.
762 * @bufferlen: Maximum size of what the the buffer will hold
763 * @buffer_actual_len: The actual size of the data after it was received
764 * @requestid: Identifier of the request
766 * Receives directly from the hyper-v vmbus and puts the data it received
767 * into Buffer. This will receive the data unparsed from hyper-v.
769 * Mainly used by Hyper-V drivers.
771 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
772 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
)
774 struct vmpacket_descriptor desc
;
780 *buffer_actual_len
= 0;
784 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
785 sizeof(struct vmpacket_descriptor
));
789 packetlen
= desc
.len8
<< 3;
790 userlen
= packetlen
- (desc
.offset8
<< 3);
792 *buffer_actual_len
= userlen
;
794 if (userlen
> bufferlen
) {
796 pr_err("Buffer too small - got %d needs %d\n",
801 *requestid
= desc
.trans_id
;
803 /* Copy over the packet to the user buffer */
804 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, userlen
,
805 (desc
.offset8
<< 3), &signal
);
808 vmbus_setevent(channel
);
812 EXPORT_SYMBOL(vmbus_recvpacket
);
815 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
817 int vmbus_recvpacket_raw(struct vmbus_channel
*channel
, void *buffer
,
818 u32 bufferlen
, u32
*buffer_actual_len
,
821 struct vmpacket_descriptor desc
;
826 *buffer_actual_len
= 0;
830 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
831 sizeof(struct vmpacket_descriptor
));
836 packetlen
= desc
.len8
<< 3;
838 *buffer_actual_len
= packetlen
;
840 if (packetlen
> bufferlen
) {
841 pr_err("Buffer too small - needed %d bytes but "
842 "got space for only %d bytes\n",
843 packetlen
, bufferlen
);
847 *requestid
= desc
.trans_id
;
849 /* Copy over the entire packet to the user buffer */
850 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, packetlen
, 0,
854 vmbus_setevent(channel
);
858 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);