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
;
558 /* Stop callback and cancel the timer asap */
559 channel
->onchannel_callback
= NULL
;
560 del_timer_sync(&channel
->poll_timer
);
562 /* Send a closing message */
563 info
= kmalloc(sizeof(*info
) +
564 sizeof(struct vmbus_channel_close_channel
), GFP_KERNEL
);
565 /* FIXME: can't do anything other than return here because the
566 * function is void */
571 msg
= (struct vmbus_channel_close_channel
*)info
->msg
;
572 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
573 msg
->child_relid
= channel
->offermsg
.child_relid
;
575 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
));
578 /* Tear down the gpadl for the channel's ring buffer */
579 if (channel
->ringbuffer_gpadlhandle
)
580 vmbus_teardown_gpadl(channel
,
581 channel
->ringbuffer_gpadlhandle
);
583 /* TODO: Send a msg to release the childRelId */
585 /* Cleanup the ring buffers for this channel */
586 hv_ringbuffer_cleanup(&channel
->outbound
);
587 hv_ringbuffer_cleanup(&channel
->inbound
);
589 free_pages((unsigned long)channel
->ringbuffer_pages
,
590 get_order(channel
->ringbuffer_pagecount
* PAGE_SIZE
));
595 * If we are closing the channel during an error path in
596 * opening the channel, don't free the channel since the
597 * caller will free the channel
600 if (channel
->state
== CHANNEL_OPEN_STATE
) {
601 spin_lock_irqsave(&vmbus_connection
.channel_lock
, flags
);
602 list_del(&channel
->listentry
);
603 spin_unlock_irqrestore(&vmbus_connection
.channel_lock
, flags
);
605 free_channel(channel
);
608 EXPORT_SYMBOL_GPL(vmbus_close
);
611 * vmbus_sendpacket() - Send the specified buffer on the given channel
612 * @channel: Pointer to vmbus_channel structure.
613 * @buffer: Pointer to the buffer you want to receive the data into.
614 * @bufferlen: Maximum size of what the the buffer will hold
615 * @requestid: Identifier of the request
616 * @type: Type of packet that is being send e.g. negotiate, time
619 * Sends data in @buffer directly to hyper-v via the vmbus
620 * This will send the data unparsed to hyper-v.
622 * Mainly used by Hyper-V drivers.
624 int vmbus_sendpacket(struct vmbus_channel
*channel
, const void *buffer
,
625 u32 bufferlen
, u64 requestid
,
626 enum vmbus_packet_type type
, u32 flags
)
628 struct vmpacket_descriptor desc
;
629 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
630 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
631 struct scatterlist bufferlist
[3];
632 u64 aligned_data
= 0;
635 dump_vmbus_channel(channel
);
637 /* Setup the descriptor */
638 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
639 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
640 /* in 8-bytes granularity */
641 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
642 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
643 desc
.trans_id
= requestid
;
645 sg_init_table(bufferlist
, 3);
646 sg_set_buf(&bufferlist
[0], &desc
, sizeof(struct vmpacket_descriptor
));
647 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
648 sg_set_buf(&bufferlist
[2], &aligned_data
,
649 packetlen_aligned
- packetlen
);
651 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3);
653 /* TODO: We should determine if this is optional */
654 if (ret
== 0 && !hv_get_ringbuffer_interrupt_mask(&channel
->outbound
))
655 vmbus_setevent(channel
);
659 EXPORT_SYMBOL(vmbus_sendpacket
);
662 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
663 * packets using a GPADL Direct packet type.
665 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
666 struct hv_page_buffer pagebuffers
[],
667 u32 pagecount
, void *buffer
, u32 bufferlen
,
672 struct vmbus_channel_packet_page_buffer desc
;
675 u32 packetlen_aligned
;
676 struct scatterlist bufferlist
[3];
677 u64 aligned_data
= 0;
679 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
682 dump_vmbus_channel(channel
);
685 * Adjust the size down since vmbus_channel_packet_page_buffer is the
686 * largest size we support
688 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
689 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
690 sizeof(struct hv_page_buffer
));
691 packetlen
= descsize
+ bufferlen
;
692 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
694 /* Setup the descriptor */
695 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
696 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
697 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
698 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
699 desc
.transactionid
= requestid
;
700 desc
.rangecount
= pagecount
;
702 for (i
= 0; i
< pagecount
; i
++) {
703 desc
.range
[i
].len
= pagebuffers
[i
].len
;
704 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
705 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
708 sg_init_table(bufferlist
, 3);
709 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
710 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
711 sg_set_buf(&bufferlist
[2], &aligned_data
,
712 packetlen_aligned
- packetlen
);
714 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3);
716 /* TODO: We should determine if this is optional */
717 if (ret
== 0 && !hv_get_ringbuffer_interrupt_mask(&channel
->outbound
))
718 vmbus_setevent(channel
);
722 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
725 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
726 * using a GPADL Direct packet type.
728 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
729 struct hv_multipage_buffer
*multi_pagebuffer
,
730 void *buffer
, u32 bufferlen
, u64 requestid
)
733 struct vmbus_channel_packet_multipage_buffer desc
;
736 u32 packetlen_aligned
;
737 struct scatterlist bufferlist
[3];
738 u64 aligned_data
= 0;
739 u32 pfncount
= NUM_PAGES_SPANNED(multi_pagebuffer
->offset
,
740 multi_pagebuffer
->len
);
742 dump_vmbus_channel(channel
);
744 if ((pfncount
< 0) || (pfncount
> MAX_MULTIPAGE_BUFFER_COUNT
))
748 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
749 * the largest size we support
751 descsize
= sizeof(struct vmbus_channel_packet_multipage_buffer
) -
752 ((MAX_MULTIPAGE_BUFFER_COUNT
- pfncount
) *
754 packetlen
= descsize
+ bufferlen
;
755 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
758 /* Setup the descriptor */
759 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
760 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
761 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
762 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
763 desc
.transactionid
= requestid
;
766 desc
.range
.len
= multi_pagebuffer
->len
;
767 desc
.range
.offset
= multi_pagebuffer
->offset
;
769 memcpy(desc
.range
.pfn_array
, multi_pagebuffer
->pfn_array
,
770 pfncount
* sizeof(u64
));
772 sg_init_table(bufferlist
, 3);
773 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
774 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
775 sg_set_buf(&bufferlist
[2], &aligned_data
,
776 packetlen_aligned
- packetlen
);
778 ret
= hv_ringbuffer_write(&channel
->outbound
, bufferlist
, 3);
780 /* TODO: We should determine if this is optional */
781 if (ret
== 0 && !hv_get_ringbuffer_interrupt_mask(&channel
->outbound
))
782 vmbus_setevent(channel
);
786 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer
);
789 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
790 * @channel: Pointer to vmbus_channel structure.
791 * @buffer: Pointer to the buffer you want to receive the data into.
792 * @bufferlen: Maximum size of what the the buffer will hold
793 * @buffer_actual_len: The actual size of the data after it was received
794 * @requestid: Identifier of the request
796 * Receives directly from the hyper-v vmbus and puts the data it received
797 * into Buffer. This will receive the data unparsed from hyper-v.
799 * Mainly used by Hyper-V drivers.
801 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
802 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
)
804 struct vmpacket_descriptor desc
;
810 *buffer_actual_len
= 0;
813 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
815 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
816 sizeof(struct vmpacket_descriptor
));
818 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
822 packetlen
= desc
.len8
<< 3;
823 userlen
= packetlen
- (desc
.offset8
<< 3);
825 *buffer_actual_len
= userlen
;
827 if (userlen
> bufferlen
) {
828 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
830 pr_err("Buffer too small - got %d needs %d\n",
835 *requestid
= desc
.trans_id
;
837 /* Copy over the packet to the user buffer */
838 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, userlen
,
839 (desc
.offset8
<< 3));
841 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
845 EXPORT_SYMBOL(vmbus_recvpacket
);
848 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
850 int vmbus_recvpacket_raw(struct vmbus_channel
*channel
, void *buffer
,
851 u32 bufferlen
, u32
*buffer_actual_len
,
854 struct vmpacket_descriptor desc
;
860 *buffer_actual_len
= 0;
863 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
865 ret
= hv_ringbuffer_peek(&channel
->inbound
, &desc
,
866 sizeof(struct vmpacket_descriptor
));
868 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
873 packetlen
= desc
.len8
<< 3;
874 userlen
= packetlen
- (desc
.offset8
<< 3);
876 *buffer_actual_len
= packetlen
;
878 if (packetlen
> bufferlen
) {
879 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
881 pr_err("Buffer too small - needed %d bytes but "
882 "got space for only %d bytes\n",
883 packetlen
, bufferlen
);
887 *requestid
= desc
.trans_id
;
889 /* Copy over the entire packet to the user buffer */
890 ret
= hv_ringbuffer_read(&channel
->inbound
, buffer
, packetlen
, 0);
892 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
895 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);
898 * vmbus_onchannel_event - Channel event callback
900 void vmbus_onchannel_event(struct vmbus_channel
*channel
)
902 dump_vmbus_channel(channel
);
904 channel
->onchannel_callback(channel
->channel_callback_context
);
906 mod_timer(&channel
->poll_timer
, jiffies
+ usecs_to_jiffies(100));
910 * vmbus_ontimer - Timer event callback
912 void vmbus_ontimer(unsigned long data
)
914 struct vmbus_channel
*channel
= (struct vmbus_channel
*)data
;
916 if (channel
->onchannel_callback
)
917 channel
->onchannel_callback(channel
->channel_callback_context
);
921 * dump_vmbus_channel- Dump vmbus channel info to the console
923 static void dump_vmbus_channel(struct vmbus_channel
*channel
)
925 DPRINT_DBG(VMBUS
, "Channel (%d)", channel
->offermsg
.child_relid
);
926 hv_dump_ring_info(&channel
->outbound
, "Outbound ");
927 hv_dump_ring_info(&channel
->inbound
, "Inbound ");