1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Microsoft Corporation.
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/wait.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/hyperv.h>
18 #include <linux/uio.h>
19 #include <linux/interrupt.h>
21 #include <asm/mshyperv.h>
23 #include "hyperv_vmbus.h"
26 * hv_gpadl_size - Return the real size of a gpadl, the size that Hyper-V uses
28 * For BUFFER gpadl, Hyper-V uses the exact same size as the guest does.
30 * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the header
31 * (because of the alignment requirement), however, the hypervisor only
32 * uses the first HV_HYP_PAGE_SIZE as the header, therefore leaving a
33 * (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap. And since there are two rings in a
34 * ringbuffer, the total size for a RING gpadl that Hyper-V uses is the
35 * total size that the guest uses minus twice of the gap size.
37 static inline u32 hv_gpadl_size(enum hv_gpadl_type type, u32 size)
43 /* The size of a ringbuffer must be page-aligned */
44 BUG_ON(size % PAGE_SIZE);
46 * Two things to notice here:
47 * 1) We're processing two ring buffers as a unit
48 * 2) We're skipping any space larger than HV_HYP_PAGE_SIZE in
49 * the first guest-size page of each of the two ring buffers.
50 * So we effectively subtract out two guest-size pages, and add
51 * back two Hyper-V size pages.
53 return size - 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE);
60 * hv_ring_gpadl_send_hvpgoffset - Calculate the send offset (in unit of
61 * HV_HYP_PAGE) in a ring gpadl based on the
64 * @offset: the offset (in bytes) where the send ringbuffer starts in the
65 * virtual address space of the guest
67 static inline u32 hv_ring_gpadl_send_hvpgoffset(u32 offset)
71 * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the
72 * header (because of the alignment requirement), however, the
73 * hypervisor only uses the first HV_HYP_PAGE_SIZE as the header,
74 * therefore leaving a (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap.
76 * And to calculate the effective send offset in gpadl, we need to
79 return (offset - (PAGE_SIZE - HV_HYP_PAGE_SIZE)) >> HV_HYP_PAGE_SHIFT;
83 * hv_gpadl_hvpfn - Return the Hyper-V page PFN of the @i th Hyper-V page in
86 * @type: the type of the gpadl
87 * @kbuffer: the pointer to the gpadl in the guest
88 * @size: the total size (in bytes) of the gpadl
89 * @send_offset: the offset (in bytes) where the send ringbuffer starts in the
90 * virtual address space of the guest
93 static inline u64 hv_gpadl_hvpfn(enum hv_gpadl_type type, void *kbuffer,
94 u32 size, u32 send_offset, int i)
96 int send_idx = hv_ring_gpadl_send_hvpgoffset(send_offset);
97 unsigned long delta = 0UL;
100 case HV_GPADL_BUFFER:
105 else if (i <= send_idx)
106 delta = PAGE_SIZE - HV_HYP_PAGE_SIZE;
108 delta = 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE);
115 return virt_to_hvpfn(kbuffer + delta + (HV_HYP_PAGE_SIZE * i));
119 * vmbus_setevent- Trigger an event notification on the specified
122 void vmbus_setevent(struct vmbus_channel *channel)
124 struct hv_monitor_page *monitorpage;
126 trace_vmbus_setevent(channel);
129 * For channels marked as in "low latency" mode
130 * bypass the monitor page mechanism.
132 if (channel->offermsg.monitor_allocated && !channel->low_latency) {
133 vmbus_send_interrupt(channel->offermsg.child_relid);
135 /* Get the child to parent monitor page */
136 monitorpage = vmbus_connection.monitor_pages[1];
138 sync_set_bit(channel->monitor_bit,
139 (unsigned long *)&monitorpage->trigger_group
140 [channel->monitor_grp].pending);
143 vmbus_set_event(channel);
146 EXPORT_SYMBOL_GPL(vmbus_setevent);
148 /* vmbus_free_ring - drop mapping of ring buffer */
149 void vmbus_free_ring(struct vmbus_channel *channel)
151 hv_ringbuffer_cleanup(&channel->outbound);
152 hv_ringbuffer_cleanup(&channel->inbound);
154 if (channel->ringbuffer_page) {
155 __free_pages(channel->ringbuffer_page,
156 get_order(channel->ringbuffer_pagecount
158 channel->ringbuffer_page = NULL;
161 EXPORT_SYMBOL_GPL(vmbus_free_ring);
163 /* vmbus_alloc_ring - allocate and map pages for ring buffer */
164 int vmbus_alloc_ring(struct vmbus_channel *newchannel,
165 u32 send_size, u32 recv_size)
170 if (send_size % PAGE_SIZE || recv_size % PAGE_SIZE)
173 /* Allocate the ring buffer */
174 order = get_order(send_size + recv_size);
175 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
176 GFP_KERNEL|__GFP_ZERO, order);
179 page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order);
184 newchannel->ringbuffer_page = page;
185 newchannel->ringbuffer_pagecount = (send_size + recv_size) >> PAGE_SHIFT;
186 newchannel->ringbuffer_send_offset = send_size >> PAGE_SHIFT;
190 EXPORT_SYMBOL_GPL(vmbus_alloc_ring);
192 /* Used for Hyper-V Socket: a guest client's connect() to the host */
193 int vmbus_send_tl_connect_request(const guid_t *shv_guest_servie_id,
194 const guid_t *shv_host_servie_id)
196 struct vmbus_channel_tl_connect_request conn_msg;
199 memset(&conn_msg, 0, sizeof(conn_msg));
200 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
201 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
202 conn_msg.host_service_id = *shv_host_servie_id;
204 ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
206 trace_vmbus_send_tl_connect_request(&conn_msg, ret);
210 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
212 static int send_modifychannel_without_ack(struct vmbus_channel *channel, u32 target_vp)
214 struct vmbus_channel_modifychannel msg;
217 memset(&msg, 0, sizeof(msg));
218 msg.header.msgtype = CHANNELMSG_MODIFYCHANNEL;
219 msg.child_relid = channel->offermsg.child_relid;
220 msg.target_vp = target_vp;
222 ret = vmbus_post_msg(&msg, sizeof(msg), true);
223 trace_vmbus_send_modifychannel(&msg, ret);
228 static int send_modifychannel_with_ack(struct vmbus_channel *channel, u32 target_vp)
230 struct vmbus_channel_modifychannel *msg;
231 struct vmbus_channel_msginfo *info;
235 info = kzalloc(sizeof(struct vmbus_channel_msginfo) +
236 sizeof(struct vmbus_channel_modifychannel),
241 init_completion(&info->waitevent);
242 info->waiting_channel = channel;
244 msg = (struct vmbus_channel_modifychannel *)info->msg;
245 msg->header.msgtype = CHANNELMSG_MODIFYCHANNEL;
246 msg->child_relid = channel->offermsg.child_relid;
247 msg->target_vp = target_vp;
249 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
250 list_add_tail(&info->msglistentry, &vmbus_connection.chn_msg_list);
251 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
253 ret = vmbus_post_msg(msg, sizeof(*msg), true);
254 trace_vmbus_send_modifychannel(msg, ret);
256 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
257 list_del(&info->msglistentry);
258 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
263 * Release channel_mutex; otherwise, vmbus_onoffer_rescind() could block on
264 * the mutex and be unable to signal the completion.
266 * See the caller target_cpu_store() for information about the usage of the
269 mutex_unlock(&vmbus_connection.channel_mutex);
270 wait_for_completion(&info->waitevent);
271 mutex_lock(&vmbus_connection.channel_mutex);
273 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
274 list_del(&info->msglistentry);
275 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
277 if (info->response.modify_response.status)
286 * Set/change the vCPU (@target_vp) the channel (@child_relid) will interrupt.
288 * CHANNELMSG_MODIFYCHANNEL messages are aynchronous. When VMbus version 5.3
289 * or later is negotiated, Hyper-V always sends an ACK in response to such a
290 * message. For VMbus version 5.2 and earlier, it never sends an ACK. With-
291 * out an ACK, we can not know when the host will stop interrupting the "old"
292 * vCPU and start interrupting the "new" vCPU for the given channel.
294 * The CHANNELMSG_MODIFYCHANNEL message type is supported since VMBus version
295 * VERSION_WIN10_V4_1.
297 int vmbus_send_modifychannel(struct vmbus_channel *channel, u32 target_vp)
299 if (vmbus_proto_version >= VERSION_WIN10_V5_3)
300 return send_modifychannel_with_ack(channel, target_vp);
301 return send_modifychannel_without_ack(channel, target_vp);
303 EXPORT_SYMBOL_GPL(vmbus_send_modifychannel);
306 * create_gpadl_header - Creates a gpadl for the specified buffer
308 static int create_gpadl_header(enum hv_gpadl_type type, void *kbuffer,
309 u32 size, u32 send_offset,
310 struct vmbus_channel_msginfo **msginfo)
314 struct vmbus_channel_gpadl_header *gpadl_header;
315 struct vmbus_channel_gpadl_body *gpadl_body;
316 struct vmbus_channel_msginfo *msgheader;
317 struct vmbus_channel_msginfo *msgbody = NULL;
320 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
322 pagecount = hv_gpadl_size(type, size) >> HV_HYP_PAGE_SHIFT;
324 /* do we need a gpadl body msg */
325 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
326 sizeof(struct vmbus_channel_gpadl_header) -
327 sizeof(struct gpa_range);
328 pfncount = pfnsize / sizeof(u64);
330 if (pagecount > pfncount) {
331 /* we need a gpadl body */
332 /* fill in the header */
333 msgsize = sizeof(struct vmbus_channel_msginfo) +
334 sizeof(struct vmbus_channel_gpadl_header) +
335 sizeof(struct gpa_range) + pfncount * sizeof(u64);
336 msgheader = kzalloc(msgsize, GFP_KERNEL);
340 INIT_LIST_HEAD(&msgheader->submsglist);
341 msgheader->msgsize = msgsize;
343 gpadl_header = (struct vmbus_channel_gpadl_header *)
345 gpadl_header->rangecount = 1;
346 gpadl_header->range_buflen = sizeof(struct gpa_range) +
347 pagecount * sizeof(u64);
348 gpadl_header->range[0].byte_offset = 0;
349 gpadl_header->range[0].byte_count = hv_gpadl_size(type, size);
350 for (i = 0; i < pfncount; i++)
351 gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn(
352 type, kbuffer, size, send_offset, i);
353 *msginfo = msgheader;
356 pfnleft = pagecount - pfncount;
358 /* how many pfns can we fit */
359 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
360 sizeof(struct vmbus_channel_gpadl_body);
361 pfncount = pfnsize / sizeof(u64);
363 /* fill in the body */
365 if (pfnleft > pfncount)
370 msgsize = sizeof(struct vmbus_channel_msginfo) +
371 sizeof(struct vmbus_channel_gpadl_body) +
372 pfncurr * sizeof(u64);
373 msgbody = kzalloc(msgsize, GFP_KERNEL);
376 struct vmbus_channel_msginfo *pos = NULL;
377 struct vmbus_channel_msginfo *tmp = NULL;
379 * Free up all the allocated messages.
381 list_for_each_entry_safe(pos, tmp,
382 &msgheader->submsglist,
385 list_del(&pos->msglistentry);
392 msgbody->msgsize = msgsize;
394 (struct vmbus_channel_gpadl_body *)msgbody->msg;
397 * Gpadl is u32 and we are using a pointer which could
399 * This is governed by the guest/host protocol and
400 * so the hypervisor guarantees that this is ok.
402 for (i = 0; i < pfncurr; i++)
403 gpadl_body->pfn[i] = hv_gpadl_hvpfn(type,
404 kbuffer, size, send_offset, pfnsum + i);
406 /* add to msg header */
407 list_add_tail(&msgbody->msglistentry,
408 &msgheader->submsglist);
413 /* everything fits in a header */
414 msgsize = sizeof(struct vmbus_channel_msginfo) +
415 sizeof(struct vmbus_channel_gpadl_header) +
416 sizeof(struct gpa_range) + pagecount * sizeof(u64);
417 msgheader = kzalloc(msgsize, GFP_KERNEL);
418 if (msgheader == NULL)
421 INIT_LIST_HEAD(&msgheader->submsglist);
422 msgheader->msgsize = msgsize;
424 gpadl_header = (struct vmbus_channel_gpadl_header *)
426 gpadl_header->rangecount = 1;
427 gpadl_header->range_buflen = sizeof(struct gpa_range) +
428 pagecount * sizeof(u64);
429 gpadl_header->range[0].byte_offset = 0;
430 gpadl_header->range[0].byte_count = hv_gpadl_size(type, size);
431 for (i = 0; i < pagecount; i++)
432 gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn(
433 type, kbuffer, size, send_offset, i);
435 *msginfo = msgheader;
446 * __vmbus_establish_gpadl - Establish a GPADL for a buffer or ringbuffer
448 * @channel: a channel
449 * @type: the type of the corresponding GPADL, only meaningful for the guest.
450 * @kbuffer: from kmalloc or vmalloc
451 * @size: page-size multiple
452 * @send_offset: the offset (in bytes) where the send ring buffer starts,
453 * should be 0 for BUFFER type gpadl
454 * @gpadl_handle: some funky thing
456 static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
457 enum hv_gpadl_type type, void *kbuffer,
458 u32 size, u32 send_offset,
461 struct vmbus_channel_gpadl_header *gpadlmsg;
462 struct vmbus_channel_gpadl_body *gpadl_body;
463 struct vmbus_channel_msginfo *msginfo = NULL;
464 struct vmbus_channel_msginfo *submsginfo, *tmp;
465 struct list_head *curr;
466 u32 next_gpadl_handle;
471 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
473 ret = create_gpadl_header(type, kbuffer, size, send_offset, &msginfo);
477 init_completion(&msginfo->waitevent);
478 msginfo->waiting_channel = channel;
480 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
481 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
482 gpadlmsg->child_relid = channel->offermsg.child_relid;
483 gpadlmsg->gpadl = next_gpadl_handle;
486 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
487 list_add_tail(&msginfo->msglistentry,
488 &vmbus_connection.chn_msg_list);
490 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
492 if (channel->rescind) {
497 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
498 sizeof(*msginfo), true);
500 trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
505 list_for_each(curr, &msginfo->submsglist) {
506 submsginfo = (struct vmbus_channel_msginfo *)curr;
508 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
510 gpadl_body->header.msgtype =
511 CHANNELMSG_GPADL_BODY;
512 gpadl_body->gpadl = next_gpadl_handle;
514 ret = vmbus_post_msg(gpadl_body,
515 submsginfo->msgsize - sizeof(*submsginfo),
518 trace_vmbus_establish_gpadl_body(gpadl_body, ret);
524 wait_for_completion(&msginfo->waitevent);
526 if (msginfo->response.gpadl_created.creation_status != 0) {
527 pr_err("Failed to establish GPADL: err = 0x%x\n",
528 msginfo->response.gpadl_created.creation_status);
534 if (channel->rescind) {
539 /* At this point, we received the gpadl created msg */
540 *gpadl_handle = gpadlmsg->gpadl;
543 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
544 list_del(&msginfo->msglistentry);
545 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
546 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
556 * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
558 * @channel: a channel
559 * @kbuffer: from kmalloc or vmalloc
560 * @size: page-size multiple
561 * @gpadl_handle: some funky thing
563 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
564 u32 size, u32 *gpadl_handle)
566 return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER, kbuffer, size,
569 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
572 * request_arr_init - Allocates memory for the requestor array. Each slot
573 * keeps track of the next available slot in the array. Initially, each
574 * slot points to the next one (as in a Linked List). The last slot
575 * does not point to anything, so its value is U64_MAX by default.
576 * @size The size of the array
578 static u64 *request_arr_init(u32 size)
583 req_arr = kcalloc(size, sizeof(u64), GFP_KERNEL);
587 for (i = 0; i < size - 1; i++)
590 /* Last slot (no more available slots) */
591 req_arr[i] = U64_MAX;
597 * vmbus_alloc_requestor - Initializes @rqstor's fields.
598 * Index 0 is the first free slot
599 * @size: Size of the requestor array
601 static int vmbus_alloc_requestor(struct vmbus_requestor *rqstor, u32 size)
604 unsigned long *bitmap;
606 rqst_arr = request_arr_init(size);
610 bitmap = bitmap_zalloc(size, GFP_KERNEL);
616 rqstor->req_arr = rqst_arr;
617 rqstor->req_bitmap = bitmap;
619 rqstor->next_request_id = 0;
620 spin_lock_init(&rqstor->req_lock);
626 * vmbus_free_requestor - Frees memory allocated for @rqstor
627 * @rqstor: Pointer to the requestor struct
629 static void vmbus_free_requestor(struct vmbus_requestor *rqstor)
631 kfree(rqstor->req_arr);
632 bitmap_free(rqstor->req_bitmap);
635 static int __vmbus_open(struct vmbus_channel *newchannel,
636 void *userdata, u32 userdatalen,
637 void (*onchannelcallback)(void *context), void *context)
639 struct vmbus_channel_open_channel *open_msg;
640 struct vmbus_channel_msginfo *open_info = NULL;
641 struct page *page = newchannel->ringbuffer_page;
642 u32 send_pages, recv_pages;
646 if (userdatalen > MAX_USER_DEFINED_BYTES)
649 send_pages = newchannel->ringbuffer_send_offset;
650 recv_pages = newchannel->ringbuffer_pagecount - send_pages;
652 if (newchannel->state != CHANNEL_OPEN_STATE)
655 /* Create and init requestor */
656 if (newchannel->rqstor_size) {
657 if (vmbus_alloc_requestor(&newchannel->requestor, newchannel->rqstor_size))
661 newchannel->state = CHANNEL_OPENING_STATE;
662 newchannel->onchannel_callback = onchannelcallback;
663 newchannel->channel_callback_context = context;
665 if (!newchannel->max_pkt_size)
666 newchannel->max_pkt_size = VMBUS_DEFAULT_MAX_PKT_SIZE;
668 err = hv_ringbuffer_init(&newchannel->outbound, page, send_pages, 0);
670 goto error_clean_ring;
672 err = hv_ringbuffer_init(&newchannel->inbound, &page[send_pages],
673 recv_pages, newchannel->max_pkt_size);
675 goto error_clean_ring;
677 /* Establish the gpadl for the ring buffer */
678 newchannel->ringbuffer_gpadlhandle = 0;
680 err = __vmbus_establish_gpadl(newchannel, HV_GPADL_RING,
681 page_address(newchannel->ringbuffer_page),
682 (send_pages + recv_pages) << PAGE_SHIFT,
683 newchannel->ringbuffer_send_offset << PAGE_SHIFT,
684 &newchannel->ringbuffer_gpadlhandle);
686 goto error_clean_ring;
688 /* Create and init the channel open message */
689 open_info = kzalloc(sizeof(*open_info) +
690 sizeof(struct vmbus_channel_open_channel),
694 goto error_free_gpadl;
697 init_completion(&open_info->waitevent);
698 open_info->waiting_channel = newchannel;
700 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
701 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
702 open_msg->openid = newchannel->offermsg.child_relid;
703 open_msg->child_relid = newchannel->offermsg.child_relid;
704 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
706 * The unit of ->downstream_ringbuffer_pageoffset is HV_HYP_PAGE and
707 * the unit of ->ringbuffer_send_offset (i.e. send_pages) is PAGE, so
708 * here we calculate it into HV_HYP_PAGE.
710 open_msg->downstream_ringbuffer_pageoffset =
711 hv_ring_gpadl_send_hvpgoffset(send_pages << PAGE_SHIFT);
712 open_msg->target_vp = hv_cpu_number_to_vp_number(newchannel->target_cpu);
715 memcpy(open_msg->userdata, userdata, userdatalen);
717 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
718 list_add_tail(&open_info->msglistentry,
719 &vmbus_connection.chn_msg_list);
720 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
722 if (newchannel->rescind) {
724 goto error_clean_msglist;
727 err = vmbus_post_msg(open_msg,
728 sizeof(struct vmbus_channel_open_channel), true);
730 trace_vmbus_open(open_msg, err);
733 goto error_clean_msglist;
735 wait_for_completion(&open_info->waitevent);
737 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
738 list_del(&open_info->msglistentry);
739 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
741 if (newchannel->rescind) {
743 goto error_free_info;
746 if (open_info->response.open_result.status) {
748 goto error_free_info;
751 newchannel->state = CHANNEL_OPENED_STATE;
756 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
757 list_del(&open_info->msglistentry);
758 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
762 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
763 newchannel->ringbuffer_gpadlhandle = 0;
765 hv_ringbuffer_cleanup(&newchannel->outbound);
766 hv_ringbuffer_cleanup(&newchannel->inbound);
767 vmbus_free_requestor(&newchannel->requestor);
768 newchannel->state = CHANNEL_OPEN_STATE;
773 * vmbus_connect_ring - Open the channel but reuse ring buffer
775 int vmbus_connect_ring(struct vmbus_channel *newchannel,
776 void (*onchannelcallback)(void *context), void *context)
778 return __vmbus_open(newchannel, NULL, 0, onchannelcallback, context);
780 EXPORT_SYMBOL_GPL(vmbus_connect_ring);
783 * vmbus_open - Open the specified channel.
785 int vmbus_open(struct vmbus_channel *newchannel,
786 u32 send_ringbuffer_size, u32 recv_ringbuffer_size,
787 void *userdata, u32 userdatalen,
788 void (*onchannelcallback)(void *context), void *context)
792 err = vmbus_alloc_ring(newchannel, send_ringbuffer_size,
793 recv_ringbuffer_size);
797 err = __vmbus_open(newchannel, userdata, userdatalen,
798 onchannelcallback, context);
800 vmbus_free_ring(newchannel);
804 EXPORT_SYMBOL_GPL(vmbus_open);
807 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
809 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
811 struct vmbus_channel_gpadl_teardown *msg;
812 struct vmbus_channel_msginfo *info;
816 info = kzalloc(sizeof(*info) +
817 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
821 init_completion(&info->waitevent);
822 info->waiting_channel = channel;
824 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
826 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
827 msg->child_relid = channel->offermsg.child_relid;
828 msg->gpadl = gpadl_handle;
830 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
831 list_add_tail(&info->msglistentry,
832 &vmbus_connection.chn_msg_list);
833 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
835 if (channel->rescind)
838 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
841 trace_vmbus_teardown_gpadl(msg, ret);
846 wait_for_completion(&info->waitevent);
850 * If the channel has been rescinded;
851 * we will be awakened by the rescind
852 * handler; set the error code to zero so we don't leak memory.
854 if (channel->rescind)
857 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
858 list_del(&info->msglistentry);
859 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
864 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
866 void vmbus_reset_channel_cb(struct vmbus_channel *channel)
871 * vmbus_on_event(), running in the per-channel tasklet, can race
872 * with vmbus_close_internal() in the case of SMP guest, e.g., when
873 * the former is accessing channel->inbound.ring_buffer, the latter
874 * could be freeing the ring_buffer pages, so here we must stop it
877 * vmbus_chan_sched() might call the netvsc driver callback function
878 * that ends up scheduling NAPI work that accesses the ring buffer.
879 * At this point, we have to ensure that any such work is completed
880 * and that the channel ring buffer is no longer being accessed, cf.
881 * the calls to napi_disable() in netvsc_device_remove().
883 tasklet_disable(&channel->callback_event);
885 /* See the inline comments in vmbus_chan_sched(). */
886 spin_lock_irqsave(&channel->sched_lock, flags);
887 channel->onchannel_callback = NULL;
888 spin_unlock_irqrestore(&channel->sched_lock, flags);
890 channel->sc_creation_callback = NULL;
892 /* Re-enable tasklet for use on re-open */
893 tasklet_enable(&channel->callback_event);
896 static int vmbus_close_internal(struct vmbus_channel *channel)
898 struct vmbus_channel_close_channel *msg;
901 vmbus_reset_channel_cb(channel);
904 * In case a device driver's probe() fails (e.g.,
905 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
906 * rescinded later (e.g., we dynamically disable an Integrated Service
907 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
908 * here we should skip most of the below cleanup work.
910 if (channel->state != CHANNEL_OPENED_STATE)
913 channel->state = CHANNEL_OPEN_STATE;
915 /* Send a closing message */
917 msg = &channel->close_msg.msg;
919 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
920 msg->child_relid = channel->offermsg.child_relid;
922 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
925 trace_vmbus_close_internal(msg, ret);
928 pr_err("Close failed: close post msg return is %d\n", ret);
930 * If we failed to post the close msg,
931 * it is perhaps better to leak memory.
935 /* Tear down the gpadl for the channel's ring buffer */
936 else if (channel->ringbuffer_gpadlhandle) {
937 ret = vmbus_teardown_gpadl(channel,
938 channel->ringbuffer_gpadlhandle);
940 pr_err("Close failed: teardown gpadl return %d\n", ret);
942 * If we failed to teardown gpadl,
943 * it is perhaps better to leak memory.
947 channel->ringbuffer_gpadlhandle = 0;
951 vmbus_free_requestor(&channel->requestor);
956 /* disconnect ring - close all channels */
957 int vmbus_disconnect_ring(struct vmbus_channel *channel)
959 struct vmbus_channel *cur_channel, *tmp;
962 if (channel->primary_channel != NULL)
965 list_for_each_entry_safe(cur_channel, tmp, &channel->sc_list, sc_list) {
966 if (cur_channel->rescind)
967 wait_for_completion(&cur_channel->rescind_event);
969 mutex_lock(&vmbus_connection.channel_mutex);
970 if (vmbus_close_internal(cur_channel) == 0) {
971 vmbus_free_ring(cur_channel);
973 if (cur_channel->rescind)
974 hv_process_channel_removal(cur_channel);
976 mutex_unlock(&vmbus_connection.channel_mutex);
980 * Now close the primary.
982 mutex_lock(&vmbus_connection.channel_mutex);
983 ret = vmbus_close_internal(channel);
984 mutex_unlock(&vmbus_connection.channel_mutex);
988 EXPORT_SYMBOL_GPL(vmbus_disconnect_ring);
991 * vmbus_close - Close the specified channel
993 void vmbus_close(struct vmbus_channel *channel)
995 if (vmbus_disconnect_ring(channel) == 0)
996 vmbus_free_ring(channel);
998 EXPORT_SYMBOL_GPL(vmbus_close);
1001 * vmbus_sendpacket() - Send the specified buffer on the given channel
1002 * @channel: Pointer to vmbus_channel structure
1003 * @buffer: Pointer to the buffer you want to send the data from.
1004 * @bufferlen: Maximum size of what the buffer holds.
1005 * @requestid: Identifier of the request
1006 * @type: Type of packet that is being sent e.g. negotiate, time
1008 * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
1010 * Sends data in @buffer directly to Hyper-V via the vmbus.
1011 * This will send the data unparsed to Hyper-V.
1013 * Mainly used by Hyper-V drivers.
1015 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
1016 u32 bufferlen, u64 requestid,
1017 enum vmbus_packet_type type, u32 flags)
1019 struct vmpacket_descriptor desc;
1020 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
1021 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
1022 struct kvec bufferlist[3];
1023 u64 aligned_data = 0;
1024 int num_vecs = ((bufferlen != 0) ? 3 : 1);
1027 /* Setup the descriptor */
1028 desc.type = type; /* VmbusPacketTypeDataInBand; */
1029 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
1030 /* in 8-bytes granularity */
1031 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
1032 desc.len8 = (u16)(packetlen_aligned >> 3);
1033 desc.trans_id = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
1035 bufferlist[0].iov_base = &desc;
1036 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
1037 bufferlist[1].iov_base = buffer;
1038 bufferlist[1].iov_len = bufferlen;
1039 bufferlist[2].iov_base = &aligned_data;
1040 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
1042 return hv_ringbuffer_write(channel, bufferlist, num_vecs, requestid);
1044 EXPORT_SYMBOL(vmbus_sendpacket);
1047 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
1048 * packets using a GPADL Direct packet type. This interface allows you
1049 * to control notifying the host. This will be useful for sending
1050 * batched data. Also the sender can control the send flags
1053 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
1054 struct hv_page_buffer pagebuffers[],
1055 u32 pagecount, void *buffer, u32 bufferlen,
1059 struct vmbus_channel_packet_page_buffer desc;
1062 u32 packetlen_aligned;
1063 struct kvec bufferlist[3];
1064 u64 aligned_data = 0;
1066 if (pagecount > MAX_PAGE_BUFFER_COUNT)
1070 * Adjust the size down since vmbus_channel_packet_page_buffer is the
1071 * largest size we support
1073 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
1074 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
1075 sizeof(struct hv_page_buffer));
1076 packetlen = descsize + bufferlen;
1077 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
1079 /* Setup the descriptor */
1080 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
1081 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
1082 desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
1083 desc.length8 = (u16)(packetlen_aligned >> 3);
1084 desc.transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
1086 desc.rangecount = pagecount;
1088 for (i = 0; i < pagecount; i++) {
1089 desc.range[i].len = pagebuffers[i].len;
1090 desc.range[i].offset = pagebuffers[i].offset;
1091 desc.range[i].pfn = pagebuffers[i].pfn;
1094 bufferlist[0].iov_base = &desc;
1095 bufferlist[0].iov_len = descsize;
1096 bufferlist[1].iov_base = buffer;
1097 bufferlist[1].iov_len = bufferlen;
1098 bufferlist[2].iov_base = &aligned_data;
1099 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
1101 return hv_ringbuffer_write(channel, bufferlist, 3, requestid);
1103 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
1106 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
1107 * using a GPADL Direct packet type.
1108 * The buffer includes the vmbus descriptor.
1110 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
1111 struct vmbus_packet_mpb_array *desc,
1113 void *buffer, u32 bufferlen, u64 requestid)
1116 u32 packetlen_aligned;
1117 struct kvec bufferlist[3];
1118 u64 aligned_data = 0;
1120 packetlen = desc_size + bufferlen;
1121 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
1123 /* Setup the descriptor */
1124 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
1125 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
1126 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
1127 desc->length8 = (u16)(packetlen_aligned >> 3);
1128 desc->transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
1130 desc->rangecount = 1;
1132 bufferlist[0].iov_base = desc;
1133 bufferlist[0].iov_len = desc_size;
1134 bufferlist[1].iov_base = buffer;
1135 bufferlist[1].iov_len = bufferlen;
1136 bufferlist[2].iov_base = &aligned_data;
1137 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
1139 return hv_ringbuffer_write(channel, bufferlist, 3, requestid);
1141 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
1144 * __vmbus_recvpacket() - Retrieve the user packet on the specified channel
1145 * @channel: Pointer to vmbus_channel structure
1146 * @buffer: Pointer to the buffer you want to receive the data into.
1147 * @bufferlen: Maximum size of what the buffer can hold.
1148 * @buffer_actual_len: The actual size of the data after it was received.
1149 * @requestid: Identifier of the request
1150 * @raw: true means keep the vmpacket_descriptor header in the received data.
1152 * Receives directly from the hyper-v vmbus and puts the data it received
1153 * into Buffer. This will receive the data unparsed from hyper-v.
1155 * Mainly used by Hyper-V drivers.
1158 __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
1159 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
1162 return hv_ringbuffer_read(channel, buffer, bufferlen,
1163 buffer_actual_len, requestid, raw);
1167 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
1168 u32 bufferlen, u32 *buffer_actual_len,
1171 return __vmbus_recvpacket(channel, buffer, bufferlen,
1172 buffer_actual_len, requestid, false);
1174 EXPORT_SYMBOL(vmbus_recvpacket);
1177 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
1179 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
1180 u32 bufferlen, u32 *buffer_actual_len,
1183 return __vmbus_recvpacket(channel, buffer, bufferlen,
1184 buffer_actual_len, requestid, true);
1186 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
1189 * vmbus_next_request_id - Returns a new request id. It is also
1190 * the index at which the guest memory address is stored.
1191 * Uses a spin lock to avoid race conditions.
1192 * @channel: Pointer to the VMbus channel struct
1193 * @rqst_add: Guest memory address to be stored in the array
1195 u64 vmbus_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
1197 struct vmbus_requestor *rqstor = &channel->requestor;
1198 unsigned long flags;
1201 /* Check rqstor has been initialized */
1202 if (!channel->rqstor_size)
1203 return VMBUS_NO_RQSTOR;
1205 spin_lock_irqsave(&rqstor->req_lock, flags);
1206 current_id = rqstor->next_request_id;
1208 /* Requestor array is full */
1209 if (current_id >= rqstor->size) {
1210 spin_unlock_irqrestore(&rqstor->req_lock, flags);
1211 return VMBUS_RQST_ERROR;
1214 rqstor->next_request_id = rqstor->req_arr[current_id];
1215 rqstor->req_arr[current_id] = rqst_addr;
1217 /* The already held spin lock provides atomicity */
1218 bitmap_set(rqstor->req_bitmap, current_id, 1);
1220 spin_unlock_irqrestore(&rqstor->req_lock, flags);
1223 * Cannot return an ID of 0, which is reserved for an unsolicited
1224 * message from Hyper-V; Hyper-V does not acknowledge (respond to)
1225 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED requests with ID of
1226 * 0 sent by the guest.
1228 return current_id + 1;
1230 EXPORT_SYMBOL_GPL(vmbus_next_request_id);
1233 * vmbus_request_addr - Returns the memory address stored at @trans_id
1234 * in @rqstor. Uses a spin lock to avoid race conditions.
1235 * @channel: Pointer to the VMbus channel struct
1236 * @trans_id: Request id sent back from Hyper-V. Becomes the requestor's
1239 u64 vmbus_request_addr(struct vmbus_channel *channel, u64 trans_id)
1241 struct vmbus_requestor *rqstor = &channel->requestor;
1242 unsigned long flags;
1245 /* Check rqstor has been initialized */
1246 if (!channel->rqstor_size)
1247 return VMBUS_NO_RQSTOR;
1249 /* Hyper-V can send an unsolicited message with ID of 0 */
1251 return VMBUS_RQST_ERROR;
1253 spin_lock_irqsave(&rqstor->req_lock, flags);
1255 /* Data corresponding to trans_id is stored at trans_id - 1 */
1258 /* Invalid trans_id */
1259 if (trans_id >= rqstor->size || !test_bit(trans_id, rqstor->req_bitmap)) {
1260 spin_unlock_irqrestore(&rqstor->req_lock, flags);
1261 return VMBUS_RQST_ERROR;
1264 req_addr = rqstor->req_arr[trans_id];
1265 rqstor->req_arr[trans_id] = rqstor->next_request_id;
1266 rqstor->next_request_id = trans_id;
1268 /* The already held spin lock provides atomicity */
1269 bitmap_clear(rqstor->req_bitmap, trans_id, 1);
1271 spin_unlock_irqrestore(&rqstor->req_lock, flags);
1274 EXPORT_SYMBOL_GPL(vmbus_request_addr);