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 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/wait.h>
25 #include <linux/slab.h>
26 #include <linux/module.h>
29 #include "vmbus_private.h"
31 #define NUM_PAGES_SPANNED(addr, len) \
32 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
34 /* Internal routines */
35 static int create_gpadl_header(
36 void *kbuffer
, /* must be phys and virt contiguous */
37 u32 size
, /* page-size multiple */
38 struct vmbus_channel_msginfo
**msginfo
,
40 static void dump_vmbus_channel(struct vmbus_channel
*channel
);
41 static void vmbus_setevent(struct vmbus_channel
*channel
);
45 static void DumpMonitorPage(struct hv_monitor_page
*MonitorPage
)
50 DPRINT_DBG(VMBUS
, "monitorPage - %p, trigger state - %d",
51 MonitorPage
, MonitorPage
->trigger_state
);
53 for (i
= 0; i
< 4; i
++)
54 DPRINT_DBG(VMBUS
, "trigger group (%d) - %llx", i
,
55 MonitorPage
->trigger_group
[i
].as_uint64
);
57 for (i
= 0; i
< 4; i
++) {
58 for (j
= 0; j
< 32; j
++) {
59 DPRINT_DBG(VMBUS
, "latency (%d)(%d) - %llx", i
, j
,
60 MonitorPage
->latency
[i
][j
]);
63 for (i
= 0; i
< 4; i
++) {
64 for (j
= 0; j
< 32; j
++) {
65 DPRINT_DBG(VMBUS
, "param-conn id (%d)(%d) - %d", i
, j
,
66 MonitorPage
->parameter
[i
][j
].connectionid
.asu32
);
67 DPRINT_DBG(VMBUS
, "param-flag (%d)(%d) - %d", i
, j
,
68 MonitorPage
->parameter
[i
][j
].flag_number
);
75 * vmbus_setevent- Trigger an event notification on the specified
78 static void vmbus_setevent(struct vmbus_channel
*channel
)
80 struct hv_monitor_page
*monitorpage
;
82 if (channel
->offermsg
.monitor_allocated
) {
83 /* Each u32 represents 32 channels */
84 set_bit(channel
->offermsg
.child_relid
& 31,
85 (unsigned long *) vmbus_connection
.send_int_page
+
86 (channel
->offermsg
.child_relid
>> 5));
88 monitorpage
= vmbus_connection
.monitor_pages
;
89 monitorpage
++; /* Get the child to parent monitor page */
91 set_bit(channel
->monitor_bit
,
92 (unsigned long *)&monitorpage
->trigger_group
93 [channel
->monitor_grp
].pending
);
96 vmbus_set_event(channel
->offermsg
.child_relid
);
101 static void VmbusChannelClearEvent(struct vmbus_channel
*channel
)
103 struct hv_monitor_page
*monitorPage
;
105 if (Channel
->offermsg
.monitor_allocated
) {
106 /* Each u32 represents 32 channels */
107 clear_bit(Channel
->offermsg
.child_relid
& 31,
108 (unsigned long *)vmbus_connection
.send_int_page
+
109 (Channel
->offermsg
.child_relid
>> 5));
111 monitorPage
= (struct hv_monitor_page
*)
112 vmbus_connection
.monitor_pages
;
113 monitorPage
++; /* Get the child to parent monitor page */
115 clear_bit(Channel
->monitor_bit
,
116 (unsigned long *)&monitorPage
->trigger_group
117 [Channel
->monitor_grp
].Pending
);
123 * vmbus_get_debug_info -Retrieve various channel debug info
125 void vmbus_get_debug_info(struct vmbus_channel
*channel
,
126 struct vmbus_channel_debug_info
*debuginfo
)
128 struct hv_monitor_page
*monitorpage
;
129 u8 monitor_group
= (u8
)channel
->offermsg
.monitorid
/ 32;
130 u8 monitor_offset
= (u8
)channel
->offermsg
.monitorid
% 32;
131 /* u32 monitorBit = 1 << monitorOffset; */
133 debuginfo
->relid
= channel
->offermsg
.child_relid
;
134 debuginfo
->state
= channel
->state
;
135 memcpy(&debuginfo
->interfacetype
,
136 &channel
->offermsg
.offer
.if_type
, sizeof(struct hv_guid
));
137 memcpy(&debuginfo
->interface_instance
,
138 &channel
->offermsg
.offer
.if_instance
,
139 sizeof(struct hv_guid
));
141 monitorpage
= (struct hv_monitor_page
*)vmbus_connection
.monitor_pages
;
143 debuginfo
->monitorid
= channel
->offermsg
.monitorid
;
145 debuginfo
->servermonitor_pending
=
146 monitorpage
->trigger_group
[monitor_group
].pending
;
147 debuginfo
->servermonitor_latency
=
148 monitorpage
->latency
[monitor_group
][monitor_offset
];
149 debuginfo
->servermonitor_connectionid
=
150 monitorpage
->parameter
[monitor_group
]
151 [monitor_offset
].connectionid
.u
.id
;
155 debuginfo
->clientmonitor_pending
=
156 monitorpage
->trigger_group
[monitor_group
].pending
;
157 debuginfo
->clientmonitor_latency
=
158 monitorpage
->latency
[monitor_group
][monitor_offset
];
159 debuginfo
->clientmonitor_connectionid
=
160 monitorpage
->parameter
[monitor_group
]
161 [monitor_offset
].connectionid
.u
.id
;
163 ringbuffer_get_debuginfo(&channel
->inbound
, &debuginfo
->inbound
);
164 ringbuffer_get_debuginfo(&channel
->outbound
, &debuginfo
->outbound
);
168 * vmbus_open - Open the specified channel.
170 int vmbus_open(struct vmbus_channel
*newchannel
, u32 send_ringbuffer_size
,
171 u32 recv_ringbuffer_size
, void *userdata
, u32 userdatalen
,
172 void (*onchannelcallback
)(void *context
), void *context
)
174 struct vmbus_channel_open_channel
*openMsg
;
175 struct vmbus_channel_msginfo
*openInfo
= NULL
;
180 /* Aligned to page size */
181 /* ASSERT(!(SendRingBufferSize & (PAGE_SIZE - 1))); */
182 /* ASSERT(!(RecvRingBufferSize & (PAGE_SIZE - 1))); */
184 newchannel
->onchannel_callback
= onchannelcallback
;
185 newchannel
->channel_callback_context
= context
;
187 /* Allocate the ring buffer */
188 out
= (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
189 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
194 /* ASSERT(((unsigned long)out & (PAGE_SIZE-1)) == 0); */
196 in
= (void *)((unsigned long)out
+ send_ringbuffer_size
);
198 newchannel
->ringbuffer_pages
= out
;
199 newchannel
->ringbuffer_pagecount
= (send_ringbuffer_size
+
200 recv_ringbuffer_size
) >> PAGE_SHIFT
;
202 ret
= ringbuffer_init(&newchannel
->outbound
, out
, send_ringbuffer_size
);
208 ret
= ringbuffer_init(&newchannel
->inbound
, in
, recv_ringbuffer_size
);
215 /* Establish the gpadl for the ring buffer */
216 DPRINT_DBG(VMBUS
, "Establishing ring buffer's gpadl for channel %p...",
219 newchannel
->ringbuffer_gpadlhandle
= 0;
221 ret
= vmbus_establish_gpadl(newchannel
,
222 newchannel
->outbound
.ring_buffer
,
223 send_ringbuffer_size
+
224 recv_ringbuffer_size
,
225 &newchannel
->ringbuffer_gpadlhandle
);
232 DPRINT_DBG(VMBUS
, "channel %p <relid %d gpadl 0x%x send ring %p "
233 "size %d recv ring %p size %d, downstreamoffset %d>",
234 newchannel
, newchannel
->offermsg
.child_relid
,
235 newchannel
->ringbuffer_gpadlhandle
,
236 newchannel
->outbound
.ring_buffer
,
237 newchannel
->outbound
.ring_size
,
238 newchannel
->inbound
.ring_buffer
,
239 newchannel
->inbound
.ring_size
,
240 send_ringbuffer_size
);
242 /* Create and init the channel open message */
243 openInfo
= kmalloc(sizeof(*openInfo
) +
244 sizeof(struct vmbus_channel_open_channel
),
251 init_waitqueue_head(&openInfo
->waitevent
);
253 openMsg
= (struct vmbus_channel_open_channel
*)openInfo
->msg
;
254 openMsg
->header
.msgtype
= CHANNELMSG_OPENCHANNEL
;
255 openMsg
->openid
= newchannel
->offermsg
.child_relid
; /* FIXME */
256 openMsg
->child_relid
= newchannel
->offermsg
.child_relid
;
257 openMsg
->ringbuffer_gpadlhandle
= newchannel
->ringbuffer_gpadlhandle
;
258 openMsg
->downstream_ringbuffer_pageoffset
= send_ringbuffer_size
>>
260 openMsg
->server_contextarea_gpadlhandle
= 0; /* TODO */
262 if (userdatalen
> MAX_USER_DEFINED_BYTES
) {
268 memcpy(openMsg
->userdata
, userdata
, userdatalen
);
270 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
271 list_add_tail(&openInfo
->msglistentry
,
272 &vmbus_connection
.chn_msg_list
);
273 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
275 DPRINT_DBG(VMBUS
, "Sending channel open msg...");
277 ret
= vmbus_post_msg(openMsg
,
278 sizeof(struct vmbus_channel_open_channel
));
280 DPRINT_ERR(VMBUS
, "unable to open channel - %d", ret
);
284 openInfo
->wait_condition
= 0;
285 wait_event_timeout(openInfo
->waitevent
,
286 openInfo
->wait_condition
,
287 msecs_to_jiffies(1000));
288 if (openInfo
->wait_condition
== 0) {
294 if (openInfo
->response
.open_result
.status
== 0)
295 DPRINT_INFO(VMBUS
, "channel <%p> open success!!", newchannel
);
297 DPRINT_INFO(VMBUS
, "channel <%p> open failed - %d!!",
298 newchannel
, openInfo
->response
.open_result
.status
);
301 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
302 list_del(&openInfo
->msglistentry
);
303 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
309 ringbuffer_cleanup(&newchannel
->outbound
);
310 ringbuffer_cleanup(&newchannel
->inbound
);
311 free_pages((unsigned long)out
,
312 get_order(send_ringbuffer_size
+ recv_ringbuffer_size
));
316 EXPORT_SYMBOL_GPL(vmbus_open
);
319 * dump_gpadl_body - Dump the gpadl body message to the console for
320 * debugging purposes.
322 static void dump_gpadl_body(struct vmbus_channel_gpadl_body
*gpadl
, u32 len
)
327 pfncount
= (len
- sizeof(struct vmbus_channel_gpadl_body
)) /
329 DPRINT_DBG(VMBUS
, "gpadl body - len %d pfn count %d", len
, pfncount
);
331 for (i
= 0; i
< pfncount
; i
++)
332 DPRINT_DBG(VMBUS
, "gpadl body - %d) pfn %llu",
337 * dump_gpadl_header - Dump the gpadl header message to the console for
338 * debugging purposes.
340 static void dump_gpadl_header(struct vmbus_channel_gpadl_header
*gpadl
)
346 "gpadl header - relid %d, range count %d, range buflen %d",
347 gpadl
->child_relid
, gpadl
->rangecount
, gpadl
->range_buflen
);
348 for (i
= 0; i
< gpadl
->rangecount
; i
++) {
349 pagecount
= gpadl
->range
[i
].byte_count
>> PAGE_SHIFT
;
350 pagecount
= (pagecount
> 26) ? 26 : pagecount
;
352 DPRINT_DBG(VMBUS
, "gpadl range %d - len %d offset %d "
353 "page count %d", i
, gpadl
->range
[i
].byte_count
,
354 gpadl
->range
[i
].byte_offset
, pagecount
);
356 for (j
= 0; j
< pagecount
; j
++)
357 DPRINT_DBG(VMBUS
, "%d) pfn %llu", j
,
358 gpadl
->range
[i
].pfn_array
[j
]);
363 * create_gpadl_header - Creates a gpadl for the specified buffer
365 static int create_gpadl_header(void *kbuffer
, u32 size
,
366 struct vmbus_channel_msginfo
**msginfo
,
371 unsigned long long pfn
;
372 struct vmbus_channel_gpadl_header
*gpadl_header
;
373 struct vmbus_channel_gpadl_body
*gpadl_body
;
374 struct vmbus_channel_msginfo
*msgheader
;
375 struct vmbus_channel_msginfo
*msgbody
= NULL
;
378 int pfnsum
, pfncount
, pfnleft
, pfncurr
, pfnsize
;
380 /* ASSERT((kbuffer & (PAGE_SIZE-1)) == 0); */
381 /* ASSERT((Size & (PAGE_SIZE-1)) == 0); */
383 pagecount
= size
>> PAGE_SHIFT
;
384 pfn
= virt_to_phys(kbuffer
) >> PAGE_SHIFT
;
386 /* do we need a gpadl body msg */
387 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
388 sizeof(struct vmbus_channel_gpadl_header
) -
389 sizeof(struct gpa_range
);
390 pfncount
= pfnsize
/ sizeof(u64
);
392 if (pagecount
> pfncount
) {
393 /* we need a gpadl body */
394 /* fill in the header */
395 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
396 sizeof(struct vmbus_channel_gpadl_header
) +
397 sizeof(struct gpa_range
) + pfncount
* sizeof(u64
);
398 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
402 INIT_LIST_HEAD(&msgheader
->submsglist
);
403 msgheader
->msgsize
= msgsize
;
405 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
407 gpadl_header
->rangecount
= 1;
408 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
409 pagecount
* sizeof(u64
);
410 gpadl_header
->range
[0].byte_offset
= 0;
411 gpadl_header
->range
[0].byte_count
= size
;
412 for (i
= 0; i
< pfncount
; i
++)
413 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
414 *msginfo
= msgheader
;
418 pfnleft
= pagecount
- pfncount
;
420 /* how many pfns can we fit */
421 pfnsize
= MAX_SIZE_CHANNEL_MESSAGE
-
422 sizeof(struct vmbus_channel_gpadl_body
);
423 pfncount
= pfnsize
/ sizeof(u64
);
425 /* fill in the body */
427 if (pfnleft
> pfncount
)
432 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
433 sizeof(struct vmbus_channel_gpadl_body
) +
434 pfncurr
* sizeof(u64
);
435 msgbody
= kzalloc(msgsize
, GFP_KERNEL
);
436 /* FIXME: we probably need to more if this fails */
439 msgbody
->msgsize
= msgsize
;
442 (struct vmbus_channel_gpadl_body
*)msgbody
->msg
;
446 * Gpadl is u32 and we are using a pointer which could
449 /* gpadl_body->Gpadl = kbuffer; */
450 for (i
= 0; i
< pfncurr
; i
++)
451 gpadl_body
->pfn
[i
] = pfn
+ pfnsum
+ i
;
453 /* add to msg header */
454 list_add_tail(&msgbody
->msglistentry
,
455 &msgheader
->submsglist
);
460 /* everything fits in a header */
461 msgsize
= sizeof(struct vmbus_channel_msginfo
) +
462 sizeof(struct vmbus_channel_gpadl_header
) +
463 sizeof(struct gpa_range
) + pagecount
* sizeof(u64
);
464 msgheader
= kzalloc(msgsize
, GFP_KERNEL
);
465 if (msgheader
== NULL
)
467 msgheader
->msgsize
= msgsize
;
469 gpadl_header
= (struct vmbus_channel_gpadl_header
*)
471 gpadl_header
->rangecount
= 1;
472 gpadl_header
->range_buflen
= sizeof(struct gpa_range
) +
473 pagecount
* sizeof(u64
);
474 gpadl_header
->range
[0].byte_offset
= 0;
475 gpadl_header
->range
[0].byte_count
= size
;
476 for (i
= 0; i
< pagecount
; i
++)
477 gpadl_header
->range
[0].pfn_array
[i
] = pfn
+i
;
479 *msginfo
= msgheader
;
491 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
493 * @channel: a channel
494 * @kbuffer: from kmalloc
495 * @size: page-size multiple
496 * @gpadl_handle: some funky thing
498 int vmbus_establish_gpadl(struct vmbus_channel
*channel
, void *kbuffer
,
499 u32 size
, u32
*gpadl_handle
)
501 struct vmbus_channel_gpadl_header
*gpadlmsg
;
502 struct vmbus_channel_gpadl_body
*gpadl_body
;
503 /* struct vmbus_channel_gpadl_created *gpadlCreated; */
504 struct vmbus_channel_msginfo
*msginfo
= NULL
;
505 struct vmbus_channel_msginfo
*submsginfo
;
507 struct list_head
*curr
;
508 u32 next_gpadl_handle
;
512 next_gpadl_handle
= atomic_read(&vmbus_connection
.next_gpadl_handle
);
513 atomic_inc(&vmbus_connection
.next_gpadl_handle
);
515 ret
= create_gpadl_header(kbuffer
, size
, &msginfo
, &msgcount
);
519 init_waitqueue_head(&msginfo
->waitevent
);
521 gpadlmsg
= (struct vmbus_channel_gpadl_header
*)msginfo
->msg
;
522 gpadlmsg
->header
.msgtype
= CHANNELMSG_GPADL_HEADER
;
523 gpadlmsg
->child_relid
= channel
->offermsg
.child_relid
;
524 gpadlmsg
->gpadl
= next_gpadl_handle
;
526 dump_gpadl_header(gpadlmsg
);
528 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
529 list_add_tail(&msginfo
->msglistentry
,
530 &vmbus_connection
.chn_msg_list
);
532 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
533 DPRINT_DBG(VMBUS
, "buffer %p, size %d msg cnt %d",
534 kbuffer
, size
, msgcount
);
536 DPRINT_DBG(VMBUS
, "Sending GPADL Header - len %zd",
537 msginfo
->msgsize
- sizeof(*msginfo
));
539 msginfo
->wait_condition
= 0;
540 ret
= vmbus_post_msg(gpadlmsg
, msginfo
->msgsize
-
543 DPRINT_ERR(VMBUS
, "Unable to open channel - %d", ret
);
548 list_for_each(curr
, &msginfo
->submsglist
) {
550 /* FIXME: should this use list_entry() instead ? */
551 submsginfo
= (struct vmbus_channel_msginfo
*)curr
;
553 (struct vmbus_channel_gpadl_body
*)submsginfo
->msg
;
555 gpadl_body
->header
.msgtype
=
556 CHANNELMSG_GPADL_BODY
;
557 gpadl_body
->gpadl
= next_gpadl_handle
;
559 DPRINT_DBG(VMBUS
, "Sending GPADL Body - len %zd",
560 submsginfo
->msgsize
-
561 sizeof(*submsginfo
));
563 dump_gpadl_body(gpadl_body
, submsginfo
->msgsize
-
564 sizeof(*submsginfo
));
565 ret
= vmbus_post_msg(gpadl_body
,
566 submsginfo
->msgsize
-
567 sizeof(*submsginfo
));
573 wait_event_timeout(msginfo
->waitevent
,
574 msginfo
->wait_condition
,
575 msecs_to_jiffies(1000));
576 BUG_ON(msginfo
->wait_condition
== 0);
579 /* At this point, we received the gpadl created msg */
580 DPRINT_DBG(VMBUS
, "Received GPADL created "
581 "(relid %d, status %d handle %x)",
582 channel
->offermsg
.child_relid
,
583 msginfo
->response
.gpadl_created
.creation_status
,
586 *gpadl_handle
= gpadlmsg
->gpadl
;
589 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
590 list_del(&msginfo
->msglistentry
);
591 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
596 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl
);
599 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
601 int vmbus_teardown_gpadl(struct vmbus_channel
*channel
, u32 gpadl_handle
)
603 struct vmbus_channel_gpadl_teardown
*msg
;
604 struct vmbus_channel_msginfo
*info
;
608 /* ASSERT(gpadl_handle != 0); */
610 info
= kmalloc(sizeof(*info
) +
611 sizeof(struct vmbus_channel_gpadl_teardown
), GFP_KERNEL
);
615 init_waitqueue_head(&info
->waitevent
);
617 msg
= (struct vmbus_channel_gpadl_teardown
*)info
->msg
;
619 msg
->header
.msgtype
= CHANNELMSG_GPADL_TEARDOWN
;
620 msg
->child_relid
= channel
->offermsg
.child_relid
;
621 msg
->gpadl
= gpadl_handle
;
623 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
624 list_add_tail(&info
->msglistentry
,
625 &vmbus_connection
.chn_msg_list
);
626 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
627 info
->wait_condition
= 0;
628 ret
= vmbus_post_msg(msg
,
629 sizeof(struct vmbus_channel_gpadl_teardown
));
632 wait_event_timeout(info
->waitevent
,
633 info
->wait_condition
, msecs_to_jiffies(1000));
634 BUG_ON(info
->wait_condition
== 0);
636 /* Received a torndown response */
637 spin_lock_irqsave(&vmbus_connection
.channelmsg_lock
, flags
);
638 list_del(&info
->msglistentry
);
639 spin_unlock_irqrestore(&vmbus_connection
.channelmsg_lock
, flags
);
644 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl
);
647 * vmbus_close - Close the specified channel
649 void vmbus_close(struct vmbus_channel
*channel
)
651 struct vmbus_channel_close_channel
*msg
;
652 struct vmbus_channel_msginfo
*info
;
656 /* Stop callback and cancel the timer asap */
657 channel
->onchannel_callback
= NULL
;
658 del_timer_sync(&channel
->poll_timer
);
660 /* Send a closing message */
661 info
= kmalloc(sizeof(*info
) +
662 sizeof(struct vmbus_channel_close_channel
), GFP_KERNEL
);
663 /* FIXME: can't do anything other than return here because the
664 * function is void */
669 msg
= (struct vmbus_channel_close_channel
*)info
->msg
;
670 msg
->header
.msgtype
= CHANNELMSG_CLOSECHANNEL
;
671 msg
->child_relid
= channel
->offermsg
.child_relid
;
673 ret
= vmbus_post_msg(msg
, sizeof(struct vmbus_channel_close_channel
));
676 /* Tear down the gpadl for the channel's ring buffer */
677 if (channel
->ringbuffer_gpadlhandle
)
678 vmbus_teardown_gpadl(channel
,
679 channel
->ringbuffer_gpadlhandle
);
681 /* TODO: Send a msg to release the childRelId */
683 /* Cleanup the ring buffers for this channel */
684 ringbuffer_cleanup(&channel
->outbound
);
685 ringbuffer_cleanup(&channel
->inbound
);
687 free_pages((unsigned long)channel
->ringbuffer_pages
,
688 get_order(channel
->ringbuffer_pagecount
* PAGE_SIZE
));
693 * If we are closing the channel during an error path in
694 * opening the channel, don't free the channel since the
695 * caller will free the channel
698 if (channel
->state
== CHANNEL_OPEN_STATE
) {
699 spin_lock_irqsave(&vmbus_connection
.channel_lock
, flags
);
700 list_del(&channel
->listentry
);
701 spin_unlock_irqrestore(&vmbus_connection
.channel_lock
, flags
);
703 free_channel(channel
);
706 EXPORT_SYMBOL_GPL(vmbus_close
);
709 * vmbus_sendpacket() - Send the specified buffer on the given channel
710 * @channel: Pointer to vmbus_channel structure.
711 * @buffer: Pointer to the buffer you want to receive the data into.
712 * @bufferlen: Maximum size of what the the buffer will hold
713 * @requestid: Identifier of the request
714 * @type: Type of packet that is being send e.g. negotiate, time
717 * Sends data in @buffer directly to hyper-v via the vmbus
718 * This will send the data unparsed to hyper-v.
720 * Mainly used by Hyper-V drivers.
722 int vmbus_sendpacket(struct vmbus_channel
*channel
, const void *buffer
,
723 u32 bufferlen
, u64 requestid
,
724 enum vmbus_packet_type type
, u32 flags
)
726 struct vmpacket_descriptor desc
;
727 u32 packetlen
= sizeof(struct vmpacket_descriptor
) + bufferlen
;
728 u32 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
729 struct scatterlist bufferlist
[3];
730 u64 aligned_data
= 0;
733 DPRINT_DBG(VMBUS
, "channel %p buffer %p len %d",
734 channel
, buffer
, bufferlen
);
736 dump_vmbus_channel(channel
);
738 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
740 /* Setup the descriptor */
741 desc
.type
= type
; /* VmbusPacketTypeDataInBand; */
742 desc
.flags
= flags
; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
743 /* in 8-bytes granularity */
744 desc
.offset8
= sizeof(struct vmpacket_descriptor
) >> 3;
745 desc
.len8
= (u16
)(packetlen_aligned
>> 3);
746 desc
.trans_id
= requestid
;
748 sg_init_table(bufferlist
, 3);
749 sg_set_buf(&bufferlist
[0], &desc
, sizeof(struct vmpacket_descriptor
));
750 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
751 sg_set_buf(&bufferlist
[2], &aligned_data
,
752 packetlen_aligned
- packetlen
);
754 ret
= ringbuffer_write(&channel
->outbound
, bufferlist
, 3);
756 /* TODO: We should determine if this is optional */
757 if (ret
== 0 && !get_ringbuffer_interrupt_mask(&channel
->outbound
))
758 vmbus_setevent(channel
);
762 EXPORT_SYMBOL(vmbus_sendpacket
);
765 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
766 * packets using a GPADL Direct packet type.
768 int vmbus_sendpacket_pagebuffer(struct vmbus_channel
*channel
,
769 struct hv_page_buffer pagebuffers
[],
770 u32 pagecount
, void *buffer
, u32 bufferlen
,
775 struct vmbus_channel_packet_page_buffer desc
;
778 u32 packetlen_aligned
;
779 struct scatterlist bufferlist
[3];
780 u64 aligned_data
= 0;
782 if (pagecount
> MAX_PAGE_BUFFER_COUNT
)
785 dump_vmbus_channel(channel
);
788 * Adjust the size down since vmbus_channel_packet_page_buffer is the
789 * largest size we support
791 descsize
= sizeof(struct vmbus_channel_packet_page_buffer
) -
792 ((MAX_PAGE_BUFFER_COUNT
- pagecount
) *
793 sizeof(struct hv_page_buffer
));
794 packetlen
= descsize
+ bufferlen
;
795 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
797 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
799 /* Setup the descriptor */
800 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
801 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
802 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
803 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
804 desc
.transactionid
= requestid
;
805 desc
.rangecount
= pagecount
;
807 for (i
= 0; i
< pagecount
; i
++) {
808 desc
.range
[i
].len
= pagebuffers
[i
].len
;
809 desc
.range
[i
].offset
= pagebuffers
[i
].offset
;
810 desc
.range
[i
].pfn
= pagebuffers
[i
].pfn
;
813 sg_init_table(bufferlist
, 3);
814 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
815 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
816 sg_set_buf(&bufferlist
[2], &aligned_data
,
817 packetlen_aligned
- packetlen
);
819 ret
= ringbuffer_write(&channel
->outbound
, bufferlist
, 3);
821 /* TODO: We should determine if this is optional */
822 if (ret
== 0 && !get_ringbuffer_interrupt_mask(&channel
->outbound
))
823 vmbus_setevent(channel
);
827 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer
);
830 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
831 * using a GPADL Direct packet type.
833 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel
*channel
,
834 struct hv_multipage_buffer
*multi_pagebuffer
,
835 void *buffer
, u32 bufferlen
, u64 requestid
)
838 struct vmbus_channel_packet_multipage_buffer desc
;
841 u32 packetlen_aligned
;
842 struct scatterlist bufferlist
[3];
843 u64 aligned_data
= 0;
844 u32 pfncount
= NUM_PAGES_SPANNED(multi_pagebuffer
->offset
,
845 multi_pagebuffer
->len
);
847 dump_vmbus_channel(channel
);
849 DPRINT_DBG(VMBUS
, "data buffer - offset %u len %u pfn count %u",
850 multi_pagebuffer
->offset
,
851 multi_pagebuffer
->len
, pfncount
);
853 if ((pfncount
< 0) || (pfncount
> MAX_MULTIPAGE_BUFFER_COUNT
))
857 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
858 * the largest size we support
860 descsize
= sizeof(struct vmbus_channel_packet_multipage_buffer
) -
861 ((MAX_MULTIPAGE_BUFFER_COUNT
- pfncount
) *
863 packetlen
= descsize
+ bufferlen
;
864 packetlen_aligned
= ALIGN(packetlen
, sizeof(u64
));
866 /* ASSERT((packetLenAligned - packetLen) < sizeof(u64)); */
868 /* Setup the descriptor */
869 desc
.type
= VM_PKT_DATA_USING_GPA_DIRECT
;
870 desc
.flags
= VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
;
871 desc
.dataoffset8
= descsize
>> 3; /* in 8-bytes grandularity */
872 desc
.length8
= (u16
)(packetlen_aligned
>> 3);
873 desc
.transactionid
= requestid
;
876 desc
.range
.len
= multi_pagebuffer
->len
;
877 desc
.range
.offset
= multi_pagebuffer
->offset
;
879 memcpy(desc
.range
.pfn_array
, multi_pagebuffer
->pfn_array
,
880 pfncount
* sizeof(u64
));
882 sg_init_table(bufferlist
, 3);
883 sg_set_buf(&bufferlist
[0], &desc
, descsize
);
884 sg_set_buf(&bufferlist
[1], buffer
, bufferlen
);
885 sg_set_buf(&bufferlist
[2], &aligned_data
,
886 packetlen_aligned
- packetlen
);
888 ret
= ringbuffer_write(&channel
->outbound
, bufferlist
, 3);
890 /* TODO: We should determine if this is optional */
891 if (ret
== 0 && !get_ringbuffer_interrupt_mask(&channel
->outbound
))
892 vmbus_setevent(channel
);
896 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer
);
899 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
900 * @channel: Pointer to vmbus_channel structure.
901 * @buffer: Pointer to the buffer you want to receive the data into.
902 * @bufferlen: Maximum size of what the the buffer will hold
903 * @buffer_actual_len: The actual size of the data after it was received
904 * @requestid: Identifier of the request
906 * Receives directly from the hyper-v vmbus and puts the data it received
907 * into Buffer. This will receive the data unparsed from hyper-v.
909 * Mainly used by Hyper-V drivers.
911 int vmbus_recvpacket(struct vmbus_channel
*channel
, void *buffer
,
912 u32 bufferlen
, u32
*buffer_actual_len
, u64
*requestid
)
914 struct vmpacket_descriptor desc
;
920 *buffer_actual_len
= 0;
923 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
925 ret
= ringbuffer_peek(&channel
->inbound
, &desc
,
926 sizeof(struct vmpacket_descriptor
));
928 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
930 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
934 /* VmbusChannelClearEvent(Channel); */
936 packetlen
= desc
.len8
<< 3;
937 userlen
= packetlen
- (desc
.offset8
<< 3);
938 /* ASSERT(userLen > 0); */
940 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
941 "flag %d tid %llx pktlen %d datalen %d> ",
942 channel
, channel
->offermsg
.child_relid
, desc
.type
,
943 desc
.flags
, desc
.trans_id
, packetlen
, userlen
);
945 *buffer_actual_len
= userlen
;
947 if (userlen
> bufferlen
) {
948 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
950 DPRINT_ERR(VMBUS
, "buffer too small - got %d needs %d",
955 *requestid
= desc
.trans_id
;
957 /* Copy over the packet to the user buffer */
958 ret
= ringbuffer_read(&channel
->inbound
, buffer
, userlen
,
959 (desc
.offset8
<< 3));
961 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
965 EXPORT_SYMBOL(vmbus_recvpacket
);
968 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
970 int vmbus_recvpacket_raw(struct vmbus_channel
*channel
, void *buffer
,
971 u32 bufferlen
, u32
*buffer_actual_len
,
974 struct vmpacket_descriptor desc
;
980 *buffer_actual_len
= 0;
983 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
985 ret
= ringbuffer_peek(&channel
->inbound
, &desc
,
986 sizeof(struct vmpacket_descriptor
));
988 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
990 /* DPRINT_DBG(VMBUS, "nothing to read!!"); */
994 /* VmbusChannelClearEvent(Channel); */
996 packetlen
= desc
.len8
<< 3;
997 userlen
= packetlen
- (desc
.offset8
<< 3);
999 DPRINT_DBG(VMBUS
, "packet received on channel %p relid %d <type %d "
1000 "flag %d tid %llx pktlen %d datalen %d> ",
1001 channel
, channel
->offermsg
.child_relid
, desc
.type
,
1002 desc
.flags
, desc
.trans_id
, packetlen
, userlen
);
1004 *buffer_actual_len
= packetlen
;
1006 if (packetlen
> bufferlen
) {
1007 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
1009 DPRINT_ERR(VMBUS
, "buffer too small - needed %d bytes but "
1010 "got space for only %d bytes", packetlen
, bufferlen
);
1014 *requestid
= desc
.trans_id
;
1016 /* Copy over the entire packet to the user buffer */
1017 ret
= ringbuffer_read(&channel
->inbound
, buffer
, packetlen
, 0);
1019 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
1022 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw
);
1025 * vmbus_onchannel_event - Channel event callback
1027 void vmbus_onchannel_event(struct vmbus_channel
*channel
)
1029 dump_vmbus_channel(channel
);
1030 /* ASSERT(Channel->OnChannelCallback); */
1032 channel
->onchannel_callback(channel
->channel_callback_context
);
1034 mod_timer(&channel
->poll_timer
, jiffies
+ usecs_to_jiffies(100));
1038 * vmbus_ontimer - Timer event callback
1040 void vmbus_ontimer(unsigned long data
)
1042 struct vmbus_channel
*channel
= (struct vmbus_channel
*)data
;
1044 if (channel
->onchannel_callback
)
1045 channel
->onchannel_callback(channel
->channel_callback_context
);
1049 * dump_vmbus_channel- Dump vmbus channel info to the console
1051 static void dump_vmbus_channel(struct vmbus_channel
*channel
)
1053 DPRINT_DBG(VMBUS
, "Channel (%d)", channel
->offermsg
.child_relid
);
1054 dump_ring_info(&channel
->outbound
, "Outbound ");
1055 dump_ring_info(&channel
->inbound
, "Inbound ");