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>
31 #include "hyperv_vmbus.h"
33 #define NUM_PAGES_SPANNED(addr, len) \
34 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
36 /* Internal routines */
37 static int create_gpadl_header(
38 void *kbuffer
, /* must be phys and virt contiguous */
39 u32 size
, /* page-size multiple */
40 struct vmbus_channel_msginfo
**msginfo
,
42 static void vmbus_setevent(struct vmbus_channel
*channel
);
45 * vmbus_setevent- Trigger an event notification on the specified
48 static void vmbus_setevent(struct vmbus_channel
*channel
)
50 struct hv_monitor_page
*monitorpage
;
52 if (channel
->offermsg
.monitor_allocated
) {
53 /* Each u32 represents 32 channels */
54 sync_set_bit(channel
->offermsg
.child_relid
& 31,
55 (unsigned long *) vmbus_connection
.send_int_page
+
56 (channel
->offermsg
.child_relid
>> 5));
58 monitorpage
= vmbus_connection
.monitor_pages
;
59 monitorpage
++; /* Get the child to parent monitor page */
61 sync_set_bit(channel
->monitor_bit
,
62 (unsigned long *)&monitorpage
->trigger_group
63 [channel
->monitor_grp
].pending
);
66 vmbus_set_event(channel
->offermsg
.child_relid
);
71 * vmbus_get_debug_info -Retrieve various channel debug info
73 void vmbus_get_debug_info(struct vmbus_channel
*channel
,
74 struct vmbus_channel_debug_info
*debuginfo
)
76 struct hv_monitor_page
*monitorpage
;
77 u8 monitor_group
= (u8
)channel
->offermsg
.monitorid
/ 32;
78 u8 monitor_offset
= (u8
)channel
->offermsg
.monitorid
% 32;
79 /* u32 monitorBit = 1 << monitorOffset; */
81 debuginfo
->relid
= channel
->offermsg
.child_relid
;
82 debuginfo
->state
= channel
->state
;
83 memcpy(&debuginfo
->interfacetype
,
84 &channel
->offermsg
.offer
.if_type
, sizeof(struct hv_guid
));
85 memcpy(&debuginfo
->interface_instance
,
86 &channel
->offermsg
.offer
.if_instance
,
87 sizeof(struct hv_guid
));
89 monitorpage
= (struct hv_monitor_page
*)vmbus_connection
.monitor_pages
;
91 debuginfo
->monitorid
= channel
->offermsg
.monitorid
;
93 debuginfo
->servermonitor_pending
=
94 monitorpage
->trigger_group
[monitor_group
].pending
;
95 debuginfo
->servermonitor_latency
=
96 monitorpage
->latency
[monitor_group
][monitor_offset
];
97 debuginfo
->servermonitor_connectionid
=
98 monitorpage
->parameter
[monitor_group
]
99 [monitor_offset
].connectionid
.u
.id
;
103 debuginfo
->clientmonitor_pending
=
104 monitorpage
->trigger_group
[monitor_group
].pending
;
105 debuginfo
->clientmonitor_latency
=
106 monitorpage
->latency
[monitor_group
][monitor_offset
];
107 debuginfo
->clientmonitor_connectionid
=
108 monitorpage
->parameter
[monitor_group
]
109 [monitor_offset
].connectionid
.u
.id
;
111 hv_ringbuffer_get_debuginfo(&channel
->inbound
, &debuginfo
->inbound
);
112 hv_ringbuffer_get_debuginfo(&channel
->outbound
, &debuginfo
->outbound
);
116 * vmbus_open - Open the specified channel.
118 int vmbus_open(struct vmbus_channel
*newchannel
, u32 send_ringbuffer_size
,
119 u32 recv_ringbuffer_size
, void *userdata
, u32 userdatalen
,
120 void (*onchannelcallback
)(void *context
), void *context
)
122 struct vmbus_channel_open_channel
*openMsg
;
123 struct vmbus_channel_msginfo
*openInfo
= NULL
;
128 newchannel
->onchannel_callback
= onchannelcallback
;
129 newchannel
->channel_callback_context
= context
;
131 /* Allocate the ring buffer */
132 out
= (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
133 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
139 in
= (void *)((unsigned long)out
+ send_ringbuffer_size
);
141 newchannel
->ringbuffer_pages
= out
;
142 newchannel
->ringbuffer_pagecount
= (send_ringbuffer_size
+
143 recv_ringbuffer_size
) >> PAGE_SHIFT
;
145 ret
= hv_ringbuffer_init(
146 &newchannel
->outbound
, out
, send_ringbuffer_size
);
153 ret
= hv_ringbuffer_init(
154 &newchannel
->inbound
, in
, recv_ringbuffer_size
);
161 /* Establish the gpadl for the ring buffer */
162 newchannel
->ringbuffer_gpadlhandle
= 0;
164 ret
= vmbus_establish_gpadl(newchannel
,
165 newchannel
->outbound
.ring_buffer
,
166 send_ringbuffer_size
+
167 recv_ringbuffer_size
,
168 &newchannel
->ringbuffer_gpadlhandle
);
175 /* Create and init the channel open message */
176 openInfo
= kmalloc(sizeof(*openInfo
) +
177 sizeof(struct vmbus_channel_open_channel
),
184 init_completion(&openInfo
->waitevent
);
186 openMsg
= (struct vmbus_channel_open_channel
*)openInfo
->msg
;
187 openMsg
->header
.msgtype
= CHANNELMSG_OPENCHANNEL
;
188 openMsg
->openid
= newchannel
->offermsg
.child_relid
;
189 openMsg
->child_relid
= newchannel
->offermsg
.child_relid
;
190 openMsg
->ringbuffer_gpadlhandle
= newchannel
->ringbuffer_gpadlhandle
;
191 openMsg
->downstream_ringbuffer_pageoffset
= send_ringbuffer_size
>>
193 openMsg
->server_contextarea_gpadlhandle
= 0;
195 if (userdatalen
> MAX_USER_DEFINED_BYTES
) {
201 memcpy(openMsg
->userdata
, userdata
, userdatalen
);
203 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
204 list_add_tail(&openInfo
->msglistentry
,
205 &vmbus_connection
.chn_msg_list
);
206 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
208 ret
= vmbus_post_msg(openMsg
,
209 sizeof(struct vmbus_channel_open_channel
));
214 t
= wait_for_completion_timeout(&openInfo
->waitevent
, HZ
);
221 if (openInfo
->response
.open_result
.status
)
222 err
= openInfo
->response
.open_result
.status
;
225 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
226 list_del(&openInfo
->msglistentry
);
227 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
233 hv_ringbuffer_cleanup(&newchannel
->outbound
);
234 hv_ringbuffer_cleanup(&newchannel
->inbound
);
235 free_pages((unsigned long)out
,
236 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
240 EXPORT_SYMBOL_GPL(vmbus_open
);
243 * dump_gpadl_body - Dump the gpadl body message to the console for
244 * debugging purposes.
246 static void dump_gpadl_body(struct vmbus_channel_gpadl_body
*gpadl
, u32 len
)
251 pfncount
= (len
- sizeof(struct vmbus_channel_gpadl_body
)) /
254 DPRINT_DBG(VMBUS
, "gpadl body - len %d pfn count %d", len
, pfncount
);
256 for (i
= 0; i
< pfncount
; i
++)
257 DPRINT_DBG(VMBUS
, "gpadl body - %d) pfn %llu",
262 * dump_gpadl_header - Dump the gpadl header message to the console for
263 * debugging purposes.
265 static void dump_gpadl_header(struct vmbus_channel_gpadl_header
*gpadl
)
271 "gpadl header - relid %d, range count %d, range buflen %d",
272 gpadl
->child_relid
, gpadl
->rangecount
, gpadl
->range_buflen
);
273 for (i
= 0; i
< gpadl
->rangecount
; i
++) {
274 pagecount
= gpadl
->range
[i
].byte_count
>> PAGE_SHIFT
;
275 pagecount
= (pagecount
> 26) ? 26 : pagecount
;
277 DPRINT_DBG(VMBUS
, "gpadl range %d - len %d offset %d "
278 "page count %d", i
, gpadl
->range
[i
].byte_count
,
279 gpadl
->range
[i
].byte_offset
, pagecount
);
281 for (j
= 0; j
< pagecount
; j
++)
282 DPRINT_DBG(VMBUS
, "%d) pfn %llu", j
,
283 gpadl
->range
[i
].pfn_array
[j
]);
288 * create_gpadl_header - Creates a gpadl for the specified buffer
290 static int create_gpadl_header(void *kbuffer
, u32 size
,
291 struct vmbus_channel_msginfo
**msginfo
,
296 unsigned long long pfn
;
297 struct vmbus_channel_gpadl_header
*gpadl_header
;
298 struct vmbus_channel_gpadl_body
*gpadl_body
;
299 struct vmbus_channel_msginfo
*msgheader
;
300 struct vmbus_channel_msginfo
*msgbody
= NULL
;
303 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
305 pagecount
= size
>> PAGE_SHIFT
;
306 pfn
= virt_to_phys(kbuffer
) >> PAGE_SHIFT
;
308 /* do we need a gpadl body msg */
309 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
310 sizeof(struct vmbus_channel_gpadl_header
) -
311 sizeof(struct gpa_range
);
312 pfncount
= pfnsize
/ sizeof(u64
);
314 if (pagecount
> pfncount
) {
315 /* we need a gpadl body */
316 /* fill in the header */
317 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
318 sizeof(struct vmbus_channel_gpadl_header
) +
319 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
320 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
324 INIT_LIST_HEAD(&msgheader
->submsglist
);
325 msgheader
->msgsize
= msgsize
;
327 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
329 gpadl_header
->rangecount
= 1;
330 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
331 pagecount
* sizeof(u64
);
332 gpadl_header
->range
[0].byte_offset
= 0;
333 gpadl_header
->range
[0].byte_count
= size
;
334 for (i
= 0; i
< pfncount
; i
++)
335 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
336 *msginfo
= msgheader
;
340 pfnleft
= pagecount
- pfncount
;
342 /* how many pfns can we fit */
343 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
344 sizeof(struct vmbus_channel_gpadl_body
);
345 pfncount
= pfnsize
/ sizeof(u64
);
347 /* fill in the body */
349 if (pfnleft
> pfncount
)
354 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
355 sizeof(struct vmbus_channel_gpadl_body
) +
356 pfncurr
* sizeof(u64
);
357 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
358 /* FIXME: we probably need to more if this fails */
361 msgbody
->msgsize
= msgsize
;
364 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
367 * Gpadl is u32 and we are using a pointer which could
369 * This is governed by the guest/host protocol and
370 * so the hypervisor gurantees that this is ok.
372 for (i
= 0; i
< pfncurr
; i
++)
373 gpadl_body
->pfn
[i
] = pfn
+ pfnsum
+ i
;
375 /* add to msg header */
376 list_add_tail(&msgbody
->msglistentry
,
377 &msgheader
->submsglist
);
382 /* everything fits in a header */
383 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
384 sizeof(struct vmbus_channel_gpadl_header
) +
385 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
386 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
387 if (msgheader
== NULL
)
389 msgheader
->msgsize
= msgsize
;
391 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
393 gpadl_header
->rangecount
= 1;
394 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
395 pagecount
* sizeof(u64
);
396 gpadl_header
->range
[0].byte_offset
= 0;
397 gpadl_header
->range
[0].byte_count
= size
;
398 for (i
= 0; i
< pagecount
; i
++)
399 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
401 *msginfo
= msgheader
;
413 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
415 * @channel: a channel
416 * @kbuffer: from kmalloc
417 * @size: page-size multiple
418 * @gpadl_handle: some funky thing
420 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
421 u32 size
, u32
*gpadl_handle
)
423 struct vmbus_channel_gpadl_header
*gpadlmsg
;
424 struct vmbus_channel_gpadl_body
*gpadl_body
;
425 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
426 struct vmbus_channel_msginfo
*msginfo
= NULL
;
427 struct vmbus_channel_msginfo
*submsginfo
;
429 struct list_head
*curr
;
430 u32 next_gpadl_handle
;
435 next_gpadl_handle
= atomic_read(&vmbus_connection
.next_gpadl_handle
);
436 atomic_inc(&vmbus_connection
.next_gpadl_handle
);
438 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
, &msgcount
);
442 init_completion(&msginfo
->waitevent
);
444 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
445 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
446 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
447 gpadlmsg
->gpadl
= next_gpadl_handle
;
449 dump_gpadl_header(gpadlmsg
);
451 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
452 list_add_tail(&msginfo
->msglistentry
,
453 &vmbus_connection
.chn_msg_list
);
455 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
457 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
463 list_for_each(curr
, &msginfo
->submsglist
) {
465 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
467 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
469 gpadl_body
->header
.msgtype
=
470 CHANNELMSG_GPADL_BODY
;
471 gpadl_body
->gpadl
= next_gpadl_handle
;
473 dump_gpadl_body(gpadl_body
, submsginfo
->msgsize
-
474 sizeof(*submsginfo
));
475 ret
= vmbus_post_msg(gpadl_body
,
476 submsginfo
->msgsize
-
477 sizeof(*submsginfo
));
483 t
= wait_for_completion_timeout(&msginfo
->waitevent
, HZ
);
487 /* At this point, we received the gpadl created msg */
488 *gpadl_handle
= gpadlmsg
->gpadl
;
491 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
492 list_del(&msginfo
->msglistentry
);
493 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
498 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
501 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
503 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
505 struct vmbus_channel_gpadl_teardown
*msg
;
506 struct vmbus_channel_msginfo
*info
;
510 /* ASSERT(gpadl_handle != 0); */
512 info
= kmalloc(sizeof(*info
) +
513 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
517 init_completion(&info
->waitevent
);
519 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
521 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
522 msg
->child_relid
= channel
->offermsg
.child_relid
;
523 msg
->gpadl
= gpadl_handle
;
525 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
526 list_add_tail(&info
->msglistentry
,
527 &vmbus_connection
.chn_msg_list
);
528 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
529 ret
= vmbus_post_msg(msg
,
530 sizeof(struct vmbus_channel_gpadl_teardown
));
533 t
= wait_for_completion_timeout(&info
->waitevent
, HZ
);
536 /* Received a torndown response */
537 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
538 list_del(&info
->msglistentry
);
539 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
544 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
547 * vmbus_close - Close the specified channel
549 void vmbus_close(struct vmbus_channel
*channel
)
551 struct vmbus_channel_close_channel
*msg
;
554 /* Stop callback and cancel the timer asap */
555 channel
->onchannel_callback
= NULL
;
557 /* Send a closing message */
559 msg
= &channel
->close_msg
.msg
;
561 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
562 msg
->child_relid
= channel
->offermsg
.child_relid
;
564 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
));
567 /* Tear down the gpadl for the channel's ring buffer */
568 if (channel
->ringbuffer_gpadlhandle
)
569 vmbus_teardown_gpadl(channel
,
570 channel
->ringbuffer_gpadlhandle
);
572 /* Cleanup the ring buffers for this channel */
573 hv_ringbuffer_cleanup(&channel
->outbound
);
574 hv_ringbuffer_cleanup(&channel
->inbound
);
576 free_pages((unsigned long)channel
->ringbuffer_pages
,
577 get_order(channel
->ringbuffer_pagecount
* PAGE_SIZE
));
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;
609 /* Setup the descriptor */
610 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
611 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
612 /* in 8-bytes granularity */
613 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
614 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
615 desc
.trans_id
= requestid
;
617 sg_init_table(bufferlist
, 3);
618 sg_set_buf(&bufferlist
[0], &desc
, sizeof(struct vmpacket_descriptor
));
619 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
620 sg_set_buf(&bufferlist
[2], &aligned_data
,
621 packetlen_aligned
- packetlen
);
623 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3);
625 if (ret
== 0 && !hv_get_ringbuffer_interrupt_mask(&channel
->outbound
))
626 vmbus_setevent(channel
);
630 EXPORT_SYMBOL(vmbus_sendpacket
);
633 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
634 * packets using a GPADL Direct packet type.
636 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
637 struct hv_page_buffer pagebuffers
[],
638 u32 pagecount
, void *buffer
, u32 bufferlen
,
643 struct vmbus_channel_packet_page_buffer desc
;
646 u32 packetlen_aligned
;
647 struct scatterlist bufferlist
[3];
648 u64 aligned_data
= 0;
650 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
655 * Adjust the size down since vmbus_channel_packet_page_buffer is the
656 * largest size we support
658 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
659 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
660 sizeof(struct hv_page_buffer
));
661 packetlen
= descsize
+ bufferlen
;
662 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
664 /* Setup the descriptor */
665 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
666 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
667 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
668 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
669 desc
.transactionid
= requestid
;
670 desc
.rangecount
= pagecount
;
672 for (i
= 0; i
< pagecount
; i
++) {
673 desc
.range
[i
].len
= pagebuffers
[i
].len
;
674 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
675 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
678 sg_init_table(bufferlist
, 3);
679 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
680 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
681 sg_set_buf(&bufferlist
[2], &aligned_data
,
682 packetlen_aligned
- packetlen
);
684 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3);
686 if (ret
== 0 && !hv_get_ringbuffer_interrupt_mask(&channel
->outbound
))
687 vmbus_setevent(channel
);
691 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
694 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
695 * using a GPADL Direct packet type.
697 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
698 struct hv_multipage_buffer
*multi_pagebuffer
,
699 void *buffer
, u32 bufferlen
, u64 requestid
)
702 struct vmbus_channel_packet_multipage_buffer desc
;
705 u32 packetlen_aligned
;
706 struct scatterlist bufferlist
[3];
707 u64 aligned_data
= 0;
708 u32 pfncount
= NUM_PAGES_SPANNED(multi_pagebuffer
->offset
,
709 multi_pagebuffer
->len
);
712 if ((pfncount
< 0) || (pfncount
> MAX_MULTIPAGE_BUFFER_COUNT
))
716 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
717 * the largest size we support
719 descsize
= sizeof(struct vmbus_channel_packet_multipage_buffer
) -
720 ((MAX_MULTIPAGE_BUFFER_COUNT
- pfncount
) *
722 packetlen
= descsize
+ bufferlen
;
723 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
726 /* Setup the descriptor */
727 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
728 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
729 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
730 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
731 desc
.transactionid
= requestid
;
734 desc
.range
.len
= multi_pagebuffer
->len
;
735 desc
.range
.offset
= multi_pagebuffer
->offset
;
737 memcpy(desc
.range
.pfn_array
, multi_pagebuffer
->pfn_array
,
738 pfncount
* sizeof(u64
));
740 sg_init_table(bufferlist
, 3);
741 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
742 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
743 sg_set_buf(&bufferlist
[2], &aligned_data
,
744 packetlen_aligned
- packetlen
);
746 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3);
748 if (ret
== 0 && !hv_get_ringbuffer_interrupt_mask(&channel
->outbound
))
749 vmbus_setevent(channel
);
753 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer
);
756 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
757 * @channel: Pointer to vmbus_channel structure.
758 * @buffer: Pointer to the buffer you want to receive the data into.
759 * @bufferlen: Maximum size of what the the buffer will hold
760 * @buffer_actual_len: The actual size of the data after it was received
761 * @requestid: Identifier of the request
763 * Receives directly from the hyper-v vmbus and puts the data it received
764 * into Buffer. This will receive the data unparsed from hyper-v.
766 * Mainly used by Hyper-V drivers.
768 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
769 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
)
771 struct vmpacket_descriptor desc
;
777 *buffer_actual_len
= 0;
780 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
782 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
783 sizeof(struct vmpacket_descriptor
));
785 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
789 packetlen
= desc
.len8
<< 3;
790 userlen
= packetlen
- (desc
.offset8
<< 3);
792 *buffer_actual_len
= userlen
;
794 if (userlen
> bufferlen
) {
795 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
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));
808 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
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
;
827 *buffer_actual_len
= 0;
830 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
832 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
833 sizeof(struct vmpacket_descriptor
));
835 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
840 packetlen
= desc
.len8
<< 3;
841 userlen
= packetlen
- (desc
.offset8
<< 3);
843 *buffer_actual_len
= packetlen
;
845 if (packetlen
> bufferlen
) {
846 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
848 pr_err("Buffer too small - needed %d bytes but "
849 "got space for only %d bytes\n",
850 packetlen
, bufferlen
);
854 *requestid
= desc
.trans_id
;
856 /* Copy over the entire packet to the user buffer */
857 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, packetlen
, 0);
859 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
862 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);