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 dump_vmbus_channel(struct vmbus_channel
*channel
);
43 static void vmbus_setevent(struct vmbus_channel
*channel
);
46 * vmbus_setevent- Trigger an event notification on the specified
49 static void vmbus_setevent(struct vmbus_channel
*channel
)
51 struct hv_monitor_page
*monitorpage
;
53 if (channel
->offermsg
.monitor_allocated
) {
54 /* Each u32 represents 32 channels */
55 sync_set_bit(channel
->offermsg
.child_relid
& 31,
56 (unsigned long *) vmbus_connection
.send_int_page
+
57 (channel
->offermsg
.child_relid
>> 5));
59 monitorpage
= vmbus_connection
.monitor_pages
;
60 monitorpage
++; /* Get the child to parent monitor page */
62 sync_set_bit(channel
->monitor_bit
,
63 (unsigned long *)&monitorpage
->trigger_group
64 [channel
->monitor_grp
].pending
);
67 vmbus_set_event(channel
->offermsg
.child_relid
);
72 * vmbus_get_debug_info -Retrieve various channel debug info
74 void vmbus_get_debug_info(struct vmbus_channel
*channel
,
75 struct vmbus_channel_debug_info
*debuginfo
)
77 struct hv_monitor_page
*monitorpage
;
78 u8 monitor_group
= (u8
)channel
->offermsg
.monitorid
/ 32;
79 u8 monitor_offset
= (u8
)channel
->offermsg
.monitorid
% 32;
80 /* u32 monitorBit = 1 << monitorOffset; */
82 debuginfo
->relid
= channel
->offermsg
.child_relid
;
83 debuginfo
->state
= channel
->state
;
84 memcpy(&debuginfo
->interfacetype
,
85 &channel
->offermsg
.offer
.if_type
, sizeof(struct hv_guid
));
86 memcpy(&debuginfo
->interface_instance
,
87 &channel
->offermsg
.offer
.if_instance
,
88 sizeof(struct hv_guid
));
90 monitorpage
= (struct hv_monitor_page
*)vmbus_connection
.monitor_pages
;
92 debuginfo
->monitorid
= channel
->offermsg
.monitorid
;
94 debuginfo
->servermonitor_pending
=
95 monitorpage
->trigger_group
[monitor_group
].pending
;
96 debuginfo
->servermonitor_latency
=
97 monitorpage
->latency
[monitor_group
][monitor_offset
];
98 debuginfo
->servermonitor_connectionid
=
99 monitorpage
->parameter
[monitor_group
]
100 [monitor_offset
].connectionid
.u
.id
;
104 debuginfo
->clientmonitor_pending
=
105 monitorpage
->trigger_group
[monitor_group
].pending
;
106 debuginfo
->clientmonitor_latency
=
107 monitorpage
->latency
[monitor_group
][monitor_offset
];
108 debuginfo
->clientmonitor_connectionid
=
109 monitorpage
->parameter
[monitor_group
]
110 [monitor_offset
].connectionid
.u
.id
;
112 hv_ringbuffer_get_debuginfo(&channel
->inbound
, &debuginfo
->inbound
);
113 hv_ringbuffer_get_debuginfo(&channel
->outbound
, &debuginfo
->outbound
);
117 * vmbus_open - Open the specified channel.
119 int vmbus_open(struct vmbus_channel
*newchannel
, u32 send_ringbuffer_size
,
120 u32 recv_ringbuffer_size
, void *userdata
, u32 userdatalen
,
121 void (*onchannelcallback
)(void *context
), void *context
)
123 struct vmbus_channel_open_channel
*openMsg
;
124 struct vmbus_channel_msginfo
*openInfo
= NULL
;
129 newchannel
->onchannel_callback
= onchannelcallback
;
130 newchannel
->channel_callback_context
= context
;
132 /* Allocate the ring buffer */
133 out
= (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
134 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
140 in
= (void *)((unsigned long)out
+ send_ringbuffer_size
);
142 newchannel
->ringbuffer_pages
= out
;
143 newchannel
->ringbuffer_pagecount
= (send_ringbuffer_size
+
144 recv_ringbuffer_size
) >> PAGE_SHIFT
;
146 ret
= hv_ringbuffer_init(
147 &newchannel
->outbound
, out
, send_ringbuffer_size
);
154 ret
= hv_ringbuffer_init(
155 &newchannel
->inbound
, in
, recv_ringbuffer_size
);
162 /* Establish the gpadl for the ring buffer */
163 newchannel
->ringbuffer_gpadlhandle
= 0;
165 ret
= vmbus_establish_gpadl(newchannel
,
166 newchannel
->outbound
.ring_buffer
,
167 send_ringbuffer_size
+
168 recv_ringbuffer_size
,
169 &newchannel
->ringbuffer_gpadlhandle
);
176 /* Create and init the channel open message */
177 openInfo
= kmalloc(sizeof(*openInfo
) +
178 sizeof(struct vmbus_channel_open_channel
),
185 init_completion(&openInfo
->waitevent
);
187 openMsg
= (struct vmbus_channel_open_channel
*)openInfo
->msg
;
188 openMsg
->header
.msgtype
= CHANNELMSG_OPENCHANNEL
;
189 openMsg
->openid
= newchannel
->offermsg
.child_relid
; /* FIXME */
190 openMsg
->child_relid
= newchannel
->offermsg
.child_relid
;
191 openMsg
->ringbuffer_gpadlhandle
= newchannel
->ringbuffer_gpadlhandle
;
192 openMsg
->downstream_ringbuffer_pageoffset
= send_ringbuffer_size
>>
194 openMsg
->server_contextarea_gpadlhandle
= 0; /* TODO */
196 if (userdatalen
> MAX_USER_DEFINED_BYTES
) {
202 memcpy(openMsg
->userdata
, userdata
, userdatalen
);
204 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
205 list_add_tail(&openInfo
->msglistentry
,
206 &vmbus_connection
.chn_msg_list
);
207 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
209 ret
= vmbus_post_msg(openMsg
,
210 sizeof(struct vmbus_channel_open_channel
));
215 t
= wait_for_completion_timeout(&openInfo
->waitevent
, HZ
);
222 if (openInfo
->response
.open_result
.status
)
223 err
= openInfo
->response
.open_result
.status
;
226 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
227 list_del(&openInfo
->msglistentry
);
228 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
234 hv_ringbuffer_cleanup(&newchannel
->outbound
);
235 hv_ringbuffer_cleanup(&newchannel
->inbound
);
236 free_pages((unsigned long)out
,
237 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
241 EXPORT_SYMBOL_GPL(vmbus_open
);
244 * dump_gpadl_body - Dump the gpadl body message to the console for
245 * debugging purposes.
247 static void dump_gpadl_body(struct vmbus_channel_gpadl_body
*gpadl
, u32 len
)
252 pfncount
= (len
- sizeof(struct vmbus_channel_gpadl_body
)) /
255 DPRINT_DBG(VMBUS
, "gpadl body - len %d pfn count %d", len
, pfncount
);
257 for (i
= 0; i
< pfncount
; i
++)
258 DPRINT_DBG(VMBUS
, "gpadl body - %d) pfn %llu",
263 * dump_gpadl_header - Dump the gpadl header message to the console for
264 * debugging purposes.
266 static void dump_gpadl_header(struct vmbus_channel_gpadl_header
*gpadl
)
272 "gpadl header - relid %d, range count %d, range buflen %d",
273 gpadl
->child_relid
, gpadl
->rangecount
, gpadl
->range_buflen
);
274 for (i
= 0; i
< gpadl
->rangecount
; i
++) {
275 pagecount
= gpadl
->range
[i
].byte_count
>> PAGE_SHIFT
;
276 pagecount
= (pagecount
> 26) ? 26 : pagecount
;
278 DPRINT_DBG(VMBUS
, "gpadl range %d - len %d offset %d "
279 "page count %d", i
, gpadl
->range
[i
].byte_count
,
280 gpadl
->range
[i
].byte_offset
, pagecount
);
282 for (j
= 0; j
< pagecount
; j
++)
283 DPRINT_DBG(VMBUS
, "%d) pfn %llu", j
,
284 gpadl
->range
[i
].pfn_array
[j
]);
289 * create_gpadl_header - Creates a gpadl for the specified buffer
291 static int create_gpadl_header(void *kbuffer
, u32 size
,
292 struct vmbus_channel_msginfo
**msginfo
,
297 unsigned long long pfn
;
298 struct vmbus_channel_gpadl_header
*gpadl_header
;
299 struct vmbus_channel_gpadl_body
*gpadl_body
;
300 struct vmbus_channel_msginfo
*msgheader
;
301 struct vmbus_channel_msginfo
*msgbody
= NULL
;
304 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
306 pagecount
= size
>> PAGE_SHIFT
;
307 pfn
= virt_to_phys(kbuffer
) >> PAGE_SHIFT
;
309 /* do we need a gpadl body msg */
310 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
311 sizeof(struct vmbus_channel_gpadl_header
) -
312 sizeof(struct gpa_range
);
313 pfncount
= pfnsize
/ sizeof(u64
);
315 if (pagecount
> pfncount
) {
316 /* we need a gpadl body */
317 /* fill in the header */
318 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
319 sizeof(struct vmbus_channel_gpadl_header
) +
320 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
321 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
325 INIT_LIST_HEAD(&msgheader
->submsglist
);
326 msgheader
->msgsize
= msgsize
;
328 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
330 gpadl_header
->rangecount
= 1;
331 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
332 pagecount
* sizeof(u64
);
333 gpadl_header
->range
[0].byte_offset
= 0;
334 gpadl_header
->range
[0].byte_count
= size
;
335 for (i
= 0; i
< pfncount
; i
++)
336 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
337 *msginfo
= msgheader
;
341 pfnleft
= pagecount
- pfncount
;
343 /* how many pfns can we fit */
344 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
345 sizeof(struct vmbus_channel_gpadl_body
);
346 pfncount
= pfnsize
/ sizeof(u64
);
348 /* fill in the body */
350 if (pfnleft
> pfncount
)
355 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
356 sizeof(struct vmbus_channel_gpadl_body
) +
357 pfncurr
* sizeof(u64
);
358 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
359 /* FIXME: we probably need to more if this fails */
362 msgbody
->msgsize
= msgsize
;
365 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
369 * Gpadl is u32 and we are using a pointer which could
372 /* gpadl_body->Gpadl = kbuffer; */
373 for (i
= 0; i
< pfncurr
; i
++)
374 gpadl_body
->pfn
[i
] = pfn
+ pfnsum
+ i
;
376 /* add to msg header */
377 list_add_tail(&msgbody
->msglistentry
,
378 &msgheader
->submsglist
);
383 /* everything fits in a header */
384 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
385 sizeof(struct vmbus_channel_gpadl_header
) +
386 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
387 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
388 if (msgheader
== NULL
)
390 msgheader
->msgsize
= msgsize
;
392 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
394 gpadl_header
->rangecount
= 1;
395 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
396 pagecount
* sizeof(u64
);
397 gpadl_header
->range
[0].byte_offset
= 0;
398 gpadl_header
->range
[0].byte_count
= size
;
399 for (i
= 0; i
< pagecount
; i
++)
400 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
402 *msginfo
= msgheader
;
414 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
416 * @channel: a channel
417 * @kbuffer: from kmalloc
418 * @size: page-size multiple
419 * @gpadl_handle: some funky thing
421 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
422 u32 size
, u32
*gpadl_handle
)
424 struct vmbus_channel_gpadl_header
*gpadlmsg
;
425 struct vmbus_channel_gpadl_body
*gpadl_body
;
426 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
427 struct vmbus_channel_msginfo
*msginfo
= NULL
;
428 struct vmbus_channel_msginfo
*submsginfo
;
430 struct list_head
*curr
;
431 u32 next_gpadl_handle
;
436 next_gpadl_handle
= atomic_read(&vmbus_connection
.next_gpadl_handle
);
437 atomic_inc(&vmbus_connection
.next_gpadl_handle
);
439 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
, &msgcount
);
443 init_completion(&msginfo
->waitevent
);
445 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
446 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
447 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
448 gpadlmsg
->gpadl
= next_gpadl_handle
;
450 dump_gpadl_header(gpadlmsg
);
452 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
453 list_add_tail(&msginfo
->msglistentry
,
454 &vmbus_connection
.chn_msg_list
);
456 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
458 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
464 list_for_each(curr
, &msginfo
->submsglist
) {
466 /* FIXME: should this use list_entry() instead ? */
467 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
469 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
471 gpadl_body
->header
.msgtype
=
472 CHANNELMSG_GPADL_BODY
;
473 gpadl_body
->gpadl
= next_gpadl_handle
;
475 dump_gpadl_body(gpadl_body
, submsginfo
->msgsize
-
476 sizeof(*submsginfo
));
477 ret
= vmbus_post_msg(gpadl_body
,
478 submsginfo
->msgsize
-
479 sizeof(*submsginfo
));
485 t
= wait_for_completion_timeout(&msginfo
->waitevent
, HZ
);
489 /* At this point, we received the gpadl created msg */
490 *gpadl_handle
= gpadlmsg
->gpadl
;
493 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
494 list_del(&msginfo
->msglistentry
);
495 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
500 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
503 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
505 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
507 struct vmbus_channel_gpadl_teardown
*msg
;
508 struct vmbus_channel_msginfo
*info
;
512 /* ASSERT(gpadl_handle != 0); */
514 info
= kmalloc(sizeof(*info
) +
515 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
519 init_completion(&info
->waitevent
);
521 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
523 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
524 msg
->child_relid
= channel
->offermsg
.child_relid
;
525 msg
->gpadl
= gpadl_handle
;
527 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
528 list_add_tail(&info
->msglistentry
,
529 &vmbus_connection
.chn_msg_list
);
530 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
531 ret
= vmbus_post_msg(msg
,
532 sizeof(struct vmbus_channel_gpadl_teardown
));
535 t
= wait_for_completion_timeout(&info
->waitevent
, HZ
);
538 /* Received a torndown response */
539 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
540 list_del(&info
->msglistentry
);
541 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
546 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
549 * vmbus_close - Close the specified channel
551 void vmbus_close(struct vmbus_channel
*channel
)
553 struct vmbus_channel_close_channel
*msg
;
554 struct vmbus_channel_msginfo
*info
;
557 /* Stop callback and cancel the timer asap */
558 channel
->onchannel_callback
= NULL
;
559 del_timer_sync(&channel
->poll_timer
);
561 /* Send a closing message */
562 info
= kmalloc(sizeof(*info
) +
563 sizeof(struct vmbus_channel_close_channel
), GFP_KERNEL
);
564 /* FIXME: can't do anything other than return here because the
565 * function is void */
570 msg
= (struct vmbus_channel_close_channel
*)info
->msg
;
571 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
572 msg
->child_relid
= channel
->offermsg
.child_relid
;
574 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
));
577 /* Tear down the gpadl for the channel's ring buffer */
578 if (channel
->ringbuffer_gpadlhandle
)
579 vmbus_teardown_gpadl(channel
,
580 channel
->ringbuffer_gpadlhandle
);
582 /* TODO: Send a msg to release the childRelId */
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
));
594 EXPORT_SYMBOL_GPL(vmbus_close
);
597 * vmbus_sendpacket() - Send the specified buffer on the given channel
598 * @channel: Pointer to vmbus_channel structure.
599 * @buffer: Pointer to the buffer you want to receive the data into.
600 * @bufferlen: Maximum size of what the the buffer will hold
601 * @requestid: Identifier of the request
602 * @type: Type of packet that is being send e.g. negotiate, time
605 * Sends data in @buffer directly to hyper-v via the vmbus
606 * This will send the data unparsed to hyper-v.
608 * Mainly used by Hyper-V drivers.
610 int vmbus_sendpacket(struct vmbus_channel
*channel
, const void *buffer
,
611 u32 bufferlen
, u64 requestid
,
612 enum vmbus_packet_type type
, u32 flags
)
614 struct vmpacket_descriptor desc
;
615 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
616 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
617 struct scatterlist bufferlist
[3];
618 u64 aligned_data
= 0;
621 dump_vmbus_channel(channel
);
623 /* Setup the descriptor */
624 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
625 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
626 /* in 8-bytes granularity */
627 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
628 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
629 desc
.trans_id
= requestid
;
631 sg_init_table(bufferlist
, 3);
632 sg_set_buf(&bufferlist
[0], &desc
, sizeof(struct vmpacket_descriptor
));
633 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
634 sg_set_buf(&bufferlist
[2], &aligned_data
,
635 packetlen_aligned
- packetlen
);
637 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3);
639 /* TODO: We should determine if this is optional */
640 if (ret
== 0 && !hv_get_ringbuffer_interrupt_mask(&channel
->outbound
))
641 vmbus_setevent(channel
);
645 EXPORT_SYMBOL(vmbus_sendpacket
);
648 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
649 * packets using a GPADL Direct packet type.
651 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
652 struct hv_page_buffer pagebuffers
[],
653 u32 pagecount
, void *buffer
, u32 bufferlen
,
658 struct vmbus_channel_packet_page_buffer desc
;
661 u32 packetlen_aligned
;
662 struct scatterlist bufferlist
[3];
663 u64 aligned_data
= 0;
665 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
668 dump_vmbus_channel(channel
);
671 * Adjust the size down since vmbus_channel_packet_page_buffer is the
672 * largest size we support
674 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
675 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
676 sizeof(struct hv_page_buffer
));
677 packetlen
= descsize
+ bufferlen
;
678 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
680 /* Setup the descriptor */
681 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
682 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
683 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
684 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
685 desc
.transactionid
= requestid
;
686 desc
.rangecount
= pagecount
;
688 for (i
= 0; i
< pagecount
; i
++) {
689 desc
.range
[i
].len
= pagebuffers
[i
].len
;
690 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
691 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
694 sg_init_table(bufferlist
, 3);
695 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
696 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
697 sg_set_buf(&bufferlist
[2], &aligned_data
,
698 packetlen_aligned
- packetlen
);
700 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3);
702 /* TODO: We should determine if this is optional */
703 if (ret
== 0 && !hv_get_ringbuffer_interrupt_mask(&channel
->outbound
))
704 vmbus_setevent(channel
);
708 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
711 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
712 * using a GPADL Direct packet type.
714 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
715 struct hv_multipage_buffer
*multi_pagebuffer
,
716 void *buffer
, u32 bufferlen
, u64 requestid
)
719 struct vmbus_channel_packet_multipage_buffer desc
;
722 u32 packetlen_aligned
;
723 struct scatterlist bufferlist
[3];
724 u64 aligned_data
= 0;
725 u32 pfncount
= NUM_PAGES_SPANNED(multi_pagebuffer
->offset
,
726 multi_pagebuffer
->len
);
728 dump_vmbus_channel(channel
);
730 if ((pfncount
< 0) || (pfncount
> MAX_MULTIPAGE_BUFFER_COUNT
))
734 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
735 * the largest size we support
737 descsize
= sizeof(struct vmbus_channel_packet_multipage_buffer
) -
738 ((MAX_MULTIPAGE_BUFFER_COUNT
- pfncount
) *
740 packetlen
= descsize
+ bufferlen
;
741 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
744 /* Setup the descriptor */
745 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
746 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
747 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
748 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
749 desc
.transactionid
= requestid
;
752 desc
.range
.len
= multi_pagebuffer
->len
;
753 desc
.range
.offset
= multi_pagebuffer
->offset
;
755 memcpy(desc
.range
.pfn_array
, multi_pagebuffer
->pfn_array
,
756 pfncount
* sizeof(u64
));
758 sg_init_table(bufferlist
, 3);
759 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
760 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
761 sg_set_buf(&bufferlist
[2], &aligned_data
,
762 packetlen_aligned
- packetlen
);
764 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3);
766 /* TODO: We should determine if this is optional */
767 if (ret
== 0 && !hv_get_ringbuffer_interrupt_mask(&channel
->outbound
))
768 vmbus_setevent(channel
);
772 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer
);
775 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
776 * @channel: Pointer to vmbus_channel structure.
777 * @buffer: Pointer to the buffer you want to receive the data into.
778 * @bufferlen: Maximum size of what the the buffer will hold
779 * @buffer_actual_len: The actual size of the data after it was received
780 * @requestid: Identifier of the request
782 * Receives directly from the hyper-v vmbus and puts the data it received
783 * into Buffer. This will receive the data unparsed from hyper-v.
785 * Mainly used by Hyper-V drivers.
787 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
788 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
)
790 struct vmpacket_descriptor desc
;
796 *buffer_actual_len
= 0;
799 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
801 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
802 sizeof(struct vmpacket_descriptor
));
804 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
808 packetlen
= desc
.len8
<< 3;
809 userlen
= packetlen
- (desc
.offset8
<< 3);
811 *buffer_actual_len
= userlen
;
813 if (userlen
> bufferlen
) {
814 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
816 pr_err("Buffer too small - got %d needs %d\n",
821 *requestid
= desc
.trans_id
;
823 /* Copy over the packet to the user buffer */
824 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, userlen
,
825 (desc
.offset8
<< 3));
827 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
831 EXPORT_SYMBOL(vmbus_recvpacket
);
834 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
836 int vmbus_recvpacket_raw(struct vmbus_channel
*channel
, void *buffer
,
837 u32 bufferlen
, u32
*buffer_actual_len
,
840 struct vmpacket_descriptor desc
;
846 *buffer_actual_len
= 0;
849 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
851 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
852 sizeof(struct vmpacket_descriptor
));
854 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
859 packetlen
= desc
.len8
<< 3;
860 userlen
= packetlen
- (desc
.offset8
<< 3);
862 *buffer_actual_len
= packetlen
;
864 if (packetlen
> bufferlen
) {
865 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
867 pr_err("Buffer too small - needed %d bytes but "
868 "got space for only %d bytes\n",
869 packetlen
, bufferlen
);
873 *requestid
= desc
.trans_id
;
875 /* Copy over the entire packet to the user buffer */
876 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, packetlen
, 0);
878 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
881 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);
884 * vmbus_ontimer - Timer event callback
886 void vmbus_ontimer(unsigned long data
)
888 struct vmbus_channel
*channel
= (struct vmbus_channel
*)data
;
890 if (channel
->onchannel_callback
)
891 channel
->onchannel_callback(channel
->channel_callback_context
);
895 * dump_vmbus_channel- Dump vmbus channel info to the console
897 static void dump_vmbus_channel(struct vmbus_channel
*channel
)
899 DPRINT_DBG(VMBUS
, "Channel (%d)", channel
->offermsg
.child_relid
);
900 hv_dump_ring_info(&channel
->outbound
, "Outbound ");
901 hv_dump_ring_info(&channel
->inbound
, "Inbound ");