13b20eb23SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
246a97191SGreg Kroah-Hartman /*
346a97191SGreg Kroah-Hartman * Copyright (c) 2009, Microsoft Corporation.
446a97191SGreg Kroah-Hartman *
546a97191SGreg Kroah-Hartman * Authors:
646a97191SGreg Kroah-Hartman * Haiyang Zhang <haiyangz@microsoft.com>
746a97191SGreg Kroah-Hartman * Hank Janssen <hjanssen@microsoft.com>
846a97191SGreg Kroah-Hartman */
946a97191SGreg Kroah-Hartman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1046a97191SGreg Kroah-Hartman
1146a97191SGreg Kroah-Hartman #include <linux/kernel.h>
1246a97191SGreg Kroah-Hartman #include <linux/sched.h>
1346a97191SGreg Kroah-Hartman #include <linux/wait.h>
1446a97191SGreg Kroah-Hartman #include <linux/mm.h>
1546a97191SGreg Kroah-Hartman #include <linux/slab.h>
1646a97191SGreg Kroah-Hartman #include <linux/module.h>
1746a97191SGreg Kroah-Hartman #include <linux/hyperv.h>
18011a7c3cSK. Y. Srinivasan #include <linux/uio.h>
1963d55b2aSDexuan Cui #include <linux/interrupt.h>
20d4dccf35STianyu Lan #include <linux/set_memory.h>
216ba34171SMichael Kelley #include <asm/page.h>
225bf74682SAndrea Parri (Microsoft) #include <asm/mshyperv.h>
2346a97191SGreg Kroah-Hartman
2446a97191SGreg Kroah-Hartman #include "hyperv_vmbus.h"
2546a97191SGreg Kroah-Hartman
2646a97191SGreg Kroah-Hartman /*
27c1135c7fSBoqun Feng * hv_gpadl_size - Return the real size of a gpadl, the size that Hyper-V uses
28c1135c7fSBoqun Feng *
29c1135c7fSBoqun Feng * For BUFFER gpadl, Hyper-V uses the exact same size as the guest does.
30c1135c7fSBoqun Feng *
31c1135c7fSBoqun Feng * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the header
32c1135c7fSBoqun Feng * (because of the alignment requirement), however, the hypervisor only
33c1135c7fSBoqun Feng * uses the first HV_HYP_PAGE_SIZE as the header, therefore leaving a
34c1135c7fSBoqun Feng * (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap. And since there are two rings in a
35c1135c7fSBoqun Feng * ringbuffer, the total size for a RING gpadl that Hyper-V uses is the
36c1135c7fSBoqun Feng * total size that the guest uses minus twice of the gap size.
37c1135c7fSBoqun Feng */
hv_gpadl_size(enum hv_gpadl_type type,u32 size)38c1135c7fSBoqun Feng static inline u32 hv_gpadl_size(enum hv_gpadl_type type, u32 size)
39c1135c7fSBoqun Feng {
40c1135c7fSBoqun Feng switch (type) {
41c1135c7fSBoqun Feng case HV_GPADL_BUFFER:
42c1135c7fSBoqun Feng return size;
43c1135c7fSBoqun Feng case HV_GPADL_RING:
44c1135c7fSBoqun Feng /* The size of a ringbuffer must be page-aligned */
45c1135c7fSBoqun Feng BUG_ON(size % PAGE_SIZE);
46c1135c7fSBoqun Feng /*
47c1135c7fSBoqun Feng * Two things to notice here:
48c1135c7fSBoqun Feng * 1) We're processing two ring buffers as a unit
49c1135c7fSBoqun Feng * 2) We're skipping any space larger than HV_HYP_PAGE_SIZE in
50c1135c7fSBoqun Feng * the first guest-size page of each of the two ring buffers.
51c1135c7fSBoqun Feng * So we effectively subtract out two guest-size pages, and add
52c1135c7fSBoqun Feng * back two Hyper-V size pages.
53c1135c7fSBoqun Feng */
54c1135c7fSBoqun Feng return size - 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE);
55c1135c7fSBoqun Feng }
56c1135c7fSBoqun Feng BUG();
57c1135c7fSBoqun Feng return 0;
58c1135c7fSBoqun Feng }
59c1135c7fSBoqun Feng
60c1135c7fSBoqun Feng /*
61c1135c7fSBoqun Feng * hv_ring_gpadl_send_hvpgoffset - Calculate the send offset (in unit of
62c1135c7fSBoqun Feng * HV_HYP_PAGE) in a ring gpadl based on the
63c1135c7fSBoqun Feng * offset in the guest
64c1135c7fSBoqun Feng *
65c1135c7fSBoqun Feng * @offset: the offset (in bytes) where the send ringbuffer starts in the
66c1135c7fSBoqun Feng * virtual address space of the guest
67c1135c7fSBoqun Feng */
hv_ring_gpadl_send_hvpgoffset(u32 offset)68c1135c7fSBoqun Feng static inline u32 hv_ring_gpadl_send_hvpgoffset(u32 offset)
69c1135c7fSBoqun Feng {
70c1135c7fSBoqun Feng
71c1135c7fSBoqun Feng /*
72c1135c7fSBoqun Feng * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the
73c1135c7fSBoqun Feng * header (because of the alignment requirement), however, the
74c1135c7fSBoqun Feng * hypervisor only uses the first HV_HYP_PAGE_SIZE as the header,
75c1135c7fSBoqun Feng * therefore leaving a (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap.
76c1135c7fSBoqun Feng *
77c1135c7fSBoqun Feng * And to calculate the effective send offset in gpadl, we need to
78c1135c7fSBoqun Feng * substract this gap.
79c1135c7fSBoqun Feng */
80c1135c7fSBoqun Feng return (offset - (PAGE_SIZE - HV_HYP_PAGE_SIZE)) >> HV_HYP_PAGE_SHIFT;
81c1135c7fSBoqun Feng }
82c1135c7fSBoqun Feng
83c1135c7fSBoqun Feng /*
84c1135c7fSBoqun Feng * hv_gpadl_hvpfn - Return the Hyper-V page PFN of the @i th Hyper-V page in
85c1135c7fSBoqun Feng * the gpadl
86c1135c7fSBoqun Feng *
87c1135c7fSBoqun Feng * @type: the type of the gpadl
88c1135c7fSBoqun Feng * @kbuffer: the pointer to the gpadl in the guest
89c1135c7fSBoqun Feng * @size: the total size (in bytes) of the gpadl
90c1135c7fSBoqun Feng * @send_offset: the offset (in bytes) where the send ringbuffer starts in the
91c1135c7fSBoqun Feng * virtual address space of the guest
92c1135c7fSBoqun Feng * @i: the index
93c1135c7fSBoqun Feng */
hv_gpadl_hvpfn(enum hv_gpadl_type type,void * kbuffer,u32 size,u32 send_offset,int i)94c1135c7fSBoqun Feng static inline u64 hv_gpadl_hvpfn(enum hv_gpadl_type type, void *kbuffer,
95c1135c7fSBoqun Feng u32 size, u32 send_offset, int i)
96c1135c7fSBoqun Feng {
97c1135c7fSBoqun Feng int send_idx = hv_ring_gpadl_send_hvpgoffset(send_offset);
98c1135c7fSBoqun Feng unsigned long delta = 0UL;
99c1135c7fSBoqun Feng
100c1135c7fSBoqun Feng switch (type) {
101c1135c7fSBoqun Feng case HV_GPADL_BUFFER:
102c1135c7fSBoqun Feng break;
103c1135c7fSBoqun Feng case HV_GPADL_RING:
104c1135c7fSBoqun Feng if (i == 0)
105c1135c7fSBoqun Feng delta = 0;
106c1135c7fSBoqun Feng else if (i <= send_idx)
107c1135c7fSBoqun Feng delta = PAGE_SIZE - HV_HYP_PAGE_SIZE;
108c1135c7fSBoqun Feng else
109c1135c7fSBoqun Feng delta = 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE);
110c1135c7fSBoqun Feng break;
111c1135c7fSBoqun Feng default:
112c1135c7fSBoqun Feng BUG();
113c1135c7fSBoqun Feng break;
114c1135c7fSBoqun Feng }
115c1135c7fSBoqun Feng
116c1135c7fSBoqun Feng return virt_to_hvpfn(kbuffer + delta + (HV_HYP_PAGE_SIZE * i));
117c1135c7fSBoqun Feng }
118c1135c7fSBoqun Feng
119c1135c7fSBoqun Feng /*
12046a97191SGreg Kroah-Hartman * vmbus_setevent- Trigger an event notification on the specified
12146a97191SGreg Kroah-Hartman * channel.
12246a97191SGreg Kroah-Hartman */
vmbus_setevent(struct vmbus_channel * channel)1231f6ee4e7SK. Y. Srinivasan void vmbus_setevent(struct vmbus_channel *channel)
12446a97191SGreg Kroah-Hartman {
12546a97191SGreg Kroah-Hartman struct hv_monitor_page *monitorpage;
12646a97191SGreg Kroah-Hartman
127991f8f1cSVitaly Kuznetsov trace_vmbus_setevent(channel);
128991f8f1cSVitaly Kuznetsov
1293724287cSK. Y. Srinivasan /*
1303724287cSK. Y. Srinivasan * For channels marked as in "low latency" mode
1313724287cSK. Y. Srinivasan * bypass the monitor page mechanism.
1323724287cSK. Y. Srinivasan */
1335c1bec61SStephen Hemminger if (channel->offermsg.monitor_allocated && !channel->low_latency) {
1345c1bec61SStephen Hemminger vmbus_send_interrupt(channel->offermsg.child_relid);
13546a97191SGreg Kroah-Hartman
1368681db44SGreg Kroah-Hartman /* Get the child to parent monitor page */
1378681db44SGreg Kroah-Hartman monitorpage = vmbus_connection.monitor_pages[1];
13846a97191SGreg Kroah-Hartman
13946a97191SGreg Kroah-Hartman sync_set_bit(channel->monitor_bit,
14046a97191SGreg Kroah-Hartman (unsigned long *)&monitorpage->trigger_group
14146a97191SGreg Kroah-Hartman [channel->monitor_grp].pending);
14246a97191SGreg Kroah-Hartman
14346a97191SGreg Kroah-Hartman } else {
14421c3bef5SK. Y. Srinivasan vmbus_set_event(channel);
14546a97191SGreg Kroah-Hartman }
14646a97191SGreg Kroah-Hartman }
1471f6ee4e7SK. Y. Srinivasan EXPORT_SYMBOL_GPL(vmbus_setevent);
14846a97191SGreg Kroah-Hartman
149ae6935edSStephen Hemminger /* vmbus_free_ring - drop mapping of ring buffer */
vmbus_free_ring(struct vmbus_channel * channel)150ae6935edSStephen Hemminger void vmbus_free_ring(struct vmbus_channel *channel)
15146a97191SGreg Kroah-Hartman {
152ae6935edSStephen Hemminger hv_ringbuffer_cleanup(&channel->outbound);
153ae6935edSStephen Hemminger hv_ringbuffer_cleanup(&channel->inbound);
15446a97191SGreg Kroah-Hartman
155ae6935edSStephen Hemminger if (channel->ringbuffer_page) {
156*82f9e213SMichael Kelley /* In a CoCo VM leak the memory if it didn't get re-encrypted */
157*82f9e213SMichael Kelley if (!channel->ringbuffer_gpadlhandle.decrypted)
158ae6935edSStephen Hemminger __free_pages(channel->ringbuffer_page,
159ae6935edSStephen Hemminger get_order(channel->ringbuffer_pagecount
160ae6935edSStephen Hemminger << PAGE_SHIFT));
161ae6935edSStephen Hemminger channel->ringbuffer_page = NULL;
162e68d2971SK. Y. Srinivasan }
163ae6935edSStephen Hemminger }
164ae6935edSStephen Hemminger EXPORT_SYMBOL_GPL(vmbus_free_ring);
165e68d2971SK. Y. Srinivasan
166ae6935edSStephen Hemminger /* vmbus_alloc_ring - allocate and map pages for ring buffer */
vmbus_alloc_ring(struct vmbus_channel * newchannel,u32 send_size,u32 recv_size)167ae6935edSStephen Hemminger int vmbus_alloc_ring(struct vmbus_channel *newchannel,
168ae6935edSStephen Hemminger u32 send_size, u32 recv_size)
169ae6935edSStephen Hemminger {
170ae6935edSStephen Hemminger struct page *page;
171ae6935edSStephen Hemminger int order;
172ae6935edSStephen Hemminger
173ae6935edSStephen Hemminger if (send_size % PAGE_SIZE || recv_size % PAGE_SIZE)
174ae6935edSStephen Hemminger return -EINVAL;
17546a97191SGreg Kroah-Hartman
17646a97191SGreg Kroah-Hartman /* Allocate the ring buffer */
177ae6935edSStephen Hemminger order = get_order(send_size + recv_size);
178294409d2SK. Y. Srinivasan page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
17952a42c2aSStephen Hemminger GFP_KERNEL|__GFP_ZERO, order);
180294409d2SK. Y. Srinivasan
181294409d2SK. Y. Srinivasan if (!page)
18252a42c2aSStephen Hemminger page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order);
18346a97191SGreg Kroah-Hartman
184ae6935edSStephen Hemminger if (!page)
185ae6935edSStephen Hemminger return -ENOMEM;
18646a97191SGreg Kroah-Hartman
18752a42c2aSStephen Hemminger newchannel->ringbuffer_page = page;
188ae6935edSStephen Hemminger newchannel->ringbuffer_pagecount = (send_size + recv_size) >> PAGE_SHIFT;
189ae6935edSStephen Hemminger newchannel->ringbuffer_send_offset = send_size >> PAGE_SHIFT;
19046a97191SGreg Kroah-Hartman
191ae6935edSStephen Hemminger return 0;
19246a97191SGreg Kroah-Hartman }
193ae6935edSStephen Hemminger EXPORT_SYMBOL_GPL(vmbus_alloc_ring);
19446a97191SGreg Kroah-Hartman
1955c23a1a5SDexuan Cui /* Used for Hyper-V Socket: a guest client's connect() to the host */
vmbus_send_tl_connect_request(const guid_t * shv_guest_servie_id,const guid_t * shv_host_servie_id)196593db803SAndy Shevchenko int vmbus_send_tl_connect_request(const guid_t *shv_guest_servie_id,
197593db803SAndy Shevchenko const guid_t *shv_host_servie_id)
1985c23a1a5SDexuan Cui {
1995c23a1a5SDexuan Cui struct vmbus_channel_tl_connect_request conn_msg;
20098f31a00SVitaly Kuznetsov int ret;
2015c23a1a5SDexuan Cui
2025c23a1a5SDexuan Cui memset(&conn_msg, 0, sizeof(conn_msg));
2035c23a1a5SDexuan Cui conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
2045c23a1a5SDexuan Cui conn_msg.guest_endpoint_id = *shv_guest_servie_id;
2055c23a1a5SDexuan Cui conn_msg.host_service_id = *shv_host_servie_id;
2065c23a1a5SDexuan Cui
20798f31a00SVitaly Kuznetsov ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
20898f31a00SVitaly Kuznetsov
20998f31a00SVitaly Kuznetsov trace_vmbus_send_tl_connect_request(&conn_msg, ret);
21098f31a00SVitaly Kuznetsov
21198f31a00SVitaly Kuznetsov return ret;
2125c23a1a5SDexuan Cui }
2135c23a1a5SDexuan Cui EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
2145c23a1a5SDexuan Cui
send_modifychannel_without_ack(struct vmbus_channel * channel,u32 target_vp)215870ced05SAndrea Parri (Microsoft) static int send_modifychannel_without_ack(struct vmbus_channel *channel, u32 target_vp)
216870ced05SAndrea Parri (Microsoft) {
217870ced05SAndrea Parri (Microsoft) struct vmbus_channel_modifychannel msg;
218870ced05SAndrea Parri (Microsoft) int ret;
219870ced05SAndrea Parri (Microsoft)
220870ced05SAndrea Parri (Microsoft) memset(&msg, 0, sizeof(msg));
221870ced05SAndrea Parri (Microsoft) msg.header.msgtype = CHANNELMSG_MODIFYCHANNEL;
222870ced05SAndrea Parri (Microsoft) msg.child_relid = channel->offermsg.child_relid;
223870ced05SAndrea Parri (Microsoft) msg.target_vp = target_vp;
224870ced05SAndrea Parri (Microsoft)
225870ced05SAndrea Parri (Microsoft) ret = vmbus_post_msg(&msg, sizeof(msg), true);
226870ced05SAndrea Parri (Microsoft) trace_vmbus_send_modifychannel(&msg, ret);
227870ced05SAndrea Parri (Microsoft)
228870ced05SAndrea Parri (Microsoft) return ret;
229870ced05SAndrea Parri (Microsoft) }
230870ced05SAndrea Parri (Microsoft)
send_modifychannel_with_ack(struct vmbus_channel * channel,u32 target_vp)231870ced05SAndrea Parri (Microsoft) static int send_modifychannel_with_ack(struct vmbus_channel *channel, u32 target_vp)
232870ced05SAndrea Parri (Microsoft) {
233870ced05SAndrea Parri (Microsoft) struct vmbus_channel_modifychannel *msg;
234870ced05SAndrea Parri (Microsoft) struct vmbus_channel_msginfo *info;
235870ced05SAndrea Parri (Microsoft) unsigned long flags;
236870ced05SAndrea Parri (Microsoft) int ret;
237870ced05SAndrea Parri (Microsoft)
238870ced05SAndrea Parri (Microsoft) info = kzalloc(sizeof(struct vmbus_channel_msginfo) +
239870ced05SAndrea Parri (Microsoft) sizeof(struct vmbus_channel_modifychannel),
240870ced05SAndrea Parri (Microsoft) GFP_KERNEL);
241870ced05SAndrea Parri (Microsoft) if (!info)
242870ced05SAndrea Parri (Microsoft) return -ENOMEM;
243870ced05SAndrea Parri (Microsoft)
244870ced05SAndrea Parri (Microsoft) init_completion(&info->waitevent);
245870ced05SAndrea Parri (Microsoft) info->waiting_channel = channel;
246870ced05SAndrea Parri (Microsoft)
247870ced05SAndrea Parri (Microsoft) msg = (struct vmbus_channel_modifychannel *)info->msg;
248870ced05SAndrea Parri (Microsoft) msg->header.msgtype = CHANNELMSG_MODIFYCHANNEL;
249870ced05SAndrea Parri (Microsoft) msg->child_relid = channel->offermsg.child_relid;
250870ced05SAndrea Parri (Microsoft) msg->target_vp = target_vp;
251870ced05SAndrea Parri (Microsoft)
252870ced05SAndrea Parri (Microsoft) spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
253870ced05SAndrea Parri (Microsoft) list_add_tail(&info->msglistentry, &vmbus_connection.chn_msg_list);
254870ced05SAndrea Parri (Microsoft) spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
255870ced05SAndrea Parri (Microsoft)
256870ced05SAndrea Parri (Microsoft) ret = vmbus_post_msg(msg, sizeof(*msg), true);
257870ced05SAndrea Parri (Microsoft) trace_vmbus_send_modifychannel(msg, ret);
258870ced05SAndrea Parri (Microsoft) if (ret != 0) {
259870ced05SAndrea Parri (Microsoft) spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
260870ced05SAndrea Parri (Microsoft) list_del(&info->msglistentry);
261870ced05SAndrea Parri (Microsoft) spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
262870ced05SAndrea Parri (Microsoft) goto free_info;
263870ced05SAndrea Parri (Microsoft) }
264870ced05SAndrea Parri (Microsoft)
265870ced05SAndrea Parri (Microsoft) /*
266870ced05SAndrea Parri (Microsoft) * Release channel_mutex; otherwise, vmbus_onoffer_rescind() could block on
267870ced05SAndrea Parri (Microsoft) * the mutex and be unable to signal the completion.
268870ced05SAndrea Parri (Microsoft) *
269870ced05SAndrea Parri (Microsoft) * See the caller target_cpu_store() for information about the usage of the
270870ced05SAndrea Parri (Microsoft) * mutex.
271870ced05SAndrea Parri (Microsoft) */
272870ced05SAndrea Parri (Microsoft) mutex_unlock(&vmbus_connection.channel_mutex);
273870ced05SAndrea Parri (Microsoft) wait_for_completion(&info->waitevent);
274870ced05SAndrea Parri (Microsoft) mutex_lock(&vmbus_connection.channel_mutex);
275870ced05SAndrea Parri (Microsoft)
276870ced05SAndrea Parri (Microsoft) spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
277870ced05SAndrea Parri (Microsoft) list_del(&info->msglistentry);
278870ced05SAndrea Parri (Microsoft) spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
279870ced05SAndrea Parri (Microsoft)
280870ced05SAndrea Parri (Microsoft) if (info->response.modify_response.status)
281870ced05SAndrea Parri (Microsoft) ret = -EAGAIN;
282870ced05SAndrea Parri (Microsoft)
283870ced05SAndrea Parri (Microsoft) free_info:
284870ced05SAndrea Parri (Microsoft) kfree(info);
285870ced05SAndrea Parri (Microsoft) return ret;
286870ced05SAndrea Parri (Microsoft) }
287870ced05SAndrea Parri (Microsoft)
28846a97191SGreg Kroah-Hartman /*
28975278105SAndrea Parri (Microsoft) * Set/change the vCPU (@target_vp) the channel (@child_relid) will interrupt.
29075278105SAndrea Parri (Microsoft) *
291870ced05SAndrea Parri (Microsoft) * CHANNELMSG_MODIFYCHANNEL messages are aynchronous. When VMbus version 5.3
292870ced05SAndrea Parri (Microsoft) * or later is negotiated, Hyper-V always sends an ACK in response to such a
293870ced05SAndrea Parri (Microsoft) * message. For VMbus version 5.2 and earlier, it never sends an ACK. With-
294870ced05SAndrea Parri (Microsoft) * out an ACK, we can not know when the host will stop interrupting the "old"
295870ced05SAndrea Parri (Microsoft) * vCPU and start interrupting the "new" vCPU for the given channel.
29675278105SAndrea Parri (Microsoft) *
29775278105SAndrea Parri (Microsoft) * The CHANNELMSG_MODIFYCHANNEL message type is supported since VMBus version
29875278105SAndrea Parri (Microsoft) * VERSION_WIN10_V4_1.
29975278105SAndrea Parri (Microsoft) */
vmbus_send_modifychannel(struct vmbus_channel * channel,u32 target_vp)300870ced05SAndrea Parri (Microsoft) int vmbus_send_modifychannel(struct vmbus_channel *channel, u32 target_vp)
30175278105SAndrea Parri (Microsoft) {
302870ced05SAndrea Parri (Microsoft) if (vmbus_proto_version >= VERSION_WIN10_V5_3)
303870ced05SAndrea Parri (Microsoft) return send_modifychannel_with_ack(channel, target_vp);
304870ced05SAndrea Parri (Microsoft) return send_modifychannel_without_ack(channel, target_vp);
30575278105SAndrea Parri (Microsoft) }
30675278105SAndrea Parri (Microsoft) EXPORT_SYMBOL_GPL(vmbus_send_modifychannel);
30775278105SAndrea Parri (Microsoft)
30875278105SAndrea Parri (Microsoft) /*
30946a97191SGreg Kroah-Hartman * create_gpadl_header - Creates a gpadl for the specified buffer
31046a97191SGreg Kroah-Hartman */
create_gpadl_header(enum hv_gpadl_type type,void * kbuffer,u32 size,u32 send_offset,struct vmbus_channel_msginfo ** msginfo)311c1135c7fSBoqun Feng static int create_gpadl_header(enum hv_gpadl_type type, void *kbuffer,
312c1135c7fSBoqun Feng u32 size, u32 send_offset,
3134d637632SVitaly Kuznetsov struct vmbus_channel_msginfo **msginfo)
31446a97191SGreg Kroah-Hartman {
31546a97191SGreg Kroah-Hartman int i;
31646a97191SGreg Kroah-Hartman int pagecount;
31746a97191SGreg Kroah-Hartman struct vmbus_channel_gpadl_header *gpadl_header;
31846a97191SGreg Kroah-Hartman struct vmbus_channel_gpadl_body *gpadl_body;
31946a97191SGreg Kroah-Hartman struct vmbus_channel_msginfo *msgheader;
32046a97191SGreg Kroah-Hartman struct vmbus_channel_msginfo *msgbody = NULL;
32146a97191SGreg Kroah-Hartman u32 msgsize;
32246a97191SGreg Kroah-Hartman
32346a97191SGreg Kroah-Hartman int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
32446a97191SGreg Kroah-Hartman
325c1135c7fSBoqun Feng pagecount = hv_gpadl_size(type, size) >> HV_HYP_PAGE_SHIFT;
32646a97191SGreg Kroah-Hartman
32746a97191SGreg Kroah-Hartman /* do we need a gpadl body msg */
32846a97191SGreg Kroah-Hartman pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
32946a97191SGreg Kroah-Hartman sizeof(struct vmbus_channel_gpadl_header) -
33046a97191SGreg Kroah-Hartman sizeof(struct gpa_range);
33146a97191SGreg Kroah-Hartman pfncount = pfnsize / sizeof(u64);
33246a97191SGreg Kroah-Hartman
33346a97191SGreg Kroah-Hartman if (pagecount > pfncount) {
33446a97191SGreg Kroah-Hartman /* we need a gpadl body */
33546a97191SGreg Kroah-Hartman /* fill in the header */
33646a97191SGreg Kroah-Hartman msgsize = sizeof(struct vmbus_channel_msginfo) +
33746a97191SGreg Kroah-Hartman sizeof(struct vmbus_channel_gpadl_header) +
33846a97191SGreg Kroah-Hartman sizeof(struct gpa_range) + pfncount * sizeof(u64);
33946a97191SGreg Kroah-Hartman msgheader = kzalloc(msgsize, GFP_KERNEL);
34046a97191SGreg Kroah-Hartman if (!msgheader)
34146a97191SGreg Kroah-Hartman goto nomem;
34246a97191SGreg Kroah-Hartman
34346a97191SGreg Kroah-Hartman INIT_LIST_HEAD(&msgheader->submsglist);
34446a97191SGreg Kroah-Hartman msgheader->msgsize = msgsize;
34546a97191SGreg Kroah-Hartman
34646a97191SGreg Kroah-Hartman gpadl_header = (struct vmbus_channel_gpadl_header *)
34746a97191SGreg Kroah-Hartman msgheader->msg;
34846a97191SGreg Kroah-Hartman gpadl_header->rangecount = 1;
34946a97191SGreg Kroah-Hartman gpadl_header->range_buflen = sizeof(struct gpa_range) +
35046a97191SGreg Kroah-Hartman pagecount * sizeof(u64);
35146a97191SGreg Kroah-Hartman gpadl_header->range[0].byte_offset = 0;
352c1135c7fSBoqun Feng gpadl_header->range[0].byte_count = hv_gpadl_size(type, size);
35346a97191SGreg Kroah-Hartman for (i = 0; i < pfncount; i++)
354c1135c7fSBoqun Feng gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn(
355c1135c7fSBoqun Feng type, kbuffer, size, send_offset, i);
35646a97191SGreg Kroah-Hartman *msginfo = msgheader;
35746a97191SGreg Kroah-Hartman
35846a97191SGreg Kroah-Hartman pfnsum = pfncount;
35946a97191SGreg Kroah-Hartman pfnleft = pagecount - pfncount;
36046a97191SGreg Kroah-Hartman
36146a97191SGreg Kroah-Hartman /* how many pfns can we fit */
36246a97191SGreg Kroah-Hartman pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
36346a97191SGreg Kroah-Hartman sizeof(struct vmbus_channel_gpadl_body);
36446a97191SGreg Kroah-Hartman pfncount = pfnsize / sizeof(u64);
36546a97191SGreg Kroah-Hartman
36646a97191SGreg Kroah-Hartman /* fill in the body */
36746a97191SGreg Kroah-Hartman while (pfnleft) {
36846a97191SGreg Kroah-Hartman if (pfnleft > pfncount)
36946a97191SGreg Kroah-Hartman pfncurr = pfncount;
37046a97191SGreg Kroah-Hartman else
37146a97191SGreg Kroah-Hartman pfncurr = pfnleft;
37246a97191SGreg Kroah-Hartman
37346a97191SGreg Kroah-Hartman msgsize = sizeof(struct vmbus_channel_msginfo) +
37446a97191SGreg Kroah-Hartman sizeof(struct vmbus_channel_gpadl_body) +
37546a97191SGreg Kroah-Hartman pfncurr * sizeof(u64);
37646a97191SGreg Kroah-Hartman msgbody = kzalloc(msgsize, GFP_KERNEL);
37746a97191SGreg Kroah-Hartman
37846a97191SGreg Kroah-Hartman if (!msgbody) {
37946a97191SGreg Kroah-Hartman struct vmbus_channel_msginfo *pos = NULL;
38046a97191SGreg Kroah-Hartman struct vmbus_channel_msginfo *tmp = NULL;
38146a97191SGreg Kroah-Hartman /*
38246a97191SGreg Kroah-Hartman * Free up all the allocated messages.
38346a97191SGreg Kroah-Hartman */
38446a97191SGreg Kroah-Hartman list_for_each_entry_safe(pos, tmp,
38546a97191SGreg Kroah-Hartman &msgheader->submsglist,
38646a97191SGreg Kroah-Hartman msglistentry) {
38746a97191SGreg Kroah-Hartman
38846a97191SGreg Kroah-Hartman list_del(&pos->msglistentry);
38946a97191SGreg Kroah-Hartman kfree(pos);
39046a97191SGreg Kroah-Hartman }
39146a97191SGreg Kroah-Hartman
39246a97191SGreg Kroah-Hartman goto nomem;
39346a97191SGreg Kroah-Hartman }
39446a97191SGreg Kroah-Hartman
39546a97191SGreg Kroah-Hartman msgbody->msgsize = msgsize;
39646a97191SGreg Kroah-Hartman gpadl_body =
39746a97191SGreg Kroah-Hartman (struct vmbus_channel_gpadl_body *)msgbody->msg;
39846a97191SGreg Kroah-Hartman
39946a97191SGreg Kroah-Hartman /*
40046a97191SGreg Kroah-Hartman * Gpadl is u32 and we are using a pointer which could
40146a97191SGreg Kroah-Hartman * be 64-bit
40246a97191SGreg Kroah-Hartman * This is governed by the guest/host protocol and
403bdc1dd47SStephen Hemminger * so the hypervisor guarantees that this is ok.
40446a97191SGreg Kroah-Hartman */
40546a97191SGreg Kroah-Hartman for (i = 0; i < pfncurr; i++)
406c1135c7fSBoqun Feng gpadl_body->pfn[i] = hv_gpadl_hvpfn(type,
407c1135c7fSBoqun Feng kbuffer, size, send_offset, pfnsum + i);
40846a97191SGreg Kroah-Hartman
40946a97191SGreg Kroah-Hartman /* add to msg header */
41046a97191SGreg Kroah-Hartman list_add_tail(&msgbody->msglistentry,
41146a97191SGreg Kroah-Hartman &msgheader->submsglist);
41246a97191SGreg Kroah-Hartman pfnsum += pfncurr;
41346a97191SGreg Kroah-Hartman pfnleft -= pfncurr;
41446a97191SGreg Kroah-Hartman }
41546a97191SGreg Kroah-Hartman } else {
41646a97191SGreg Kroah-Hartman /* everything fits in a header */
41746a97191SGreg Kroah-Hartman msgsize = sizeof(struct vmbus_channel_msginfo) +
41846a97191SGreg Kroah-Hartman sizeof(struct vmbus_channel_gpadl_header) +
41946a97191SGreg Kroah-Hartman sizeof(struct gpa_range) + pagecount * sizeof(u64);
42046a97191SGreg Kroah-Hartman msgheader = kzalloc(msgsize, GFP_KERNEL);
42146a97191SGreg Kroah-Hartman if (msgheader == NULL)
42246a97191SGreg Kroah-Hartman goto nomem;
4234d637632SVitaly Kuznetsov
4244d637632SVitaly Kuznetsov INIT_LIST_HEAD(&msgheader->submsglist);
42546a97191SGreg Kroah-Hartman msgheader->msgsize = msgsize;
42646a97191SGreg Kroah-Hartman
42746a97191SGreg Kroah-Hartman gpadl_header = (struct vmbus_channel_gpadl_header *)
42846a97191SGreg Kroah-Hartman msgheader->msg;
42946a97191SGreg Kroah-Hartman gpadl_header->rangecount = 1;
43046a97191SGreg Kroah-Hartman gpadl_header->range_buflen = sizeof(struct gpa_range) +
43146a97191SGreg Kroah-Hartman pagecount * sizeof(u64);
43246a97191SGreg Kroah-Hartman gpadl_header->range[0].byte_offset = 0;
433c1135c7fSBoqun Feng gpadl_header->range[0].byte_count = hv_gpadl_size(type, size);
43446a97191SGreg Kroah-Hartman for (i = 0; i < pagecount; i++)
435c1135c7fSBoqun Feng gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn(
436c1135c7fSBoqun Feng type, kbuffer, size, send_offset, i);
43746a97191SGreg Kroah-Hartman
43846a97191SGreg Kroah-Hartman *msginfo = msgheader;
43946a97191SGreg Kroah-Hartman }
44046a97191SGreg Kroah-Hartman
44146a97191SGreg Kroah-Hartman return 0;
44246a97191SGreg Kroah-Hartman nomem:
44346a97191SGreg Kroah-Hartman kfree(msgheader);
44446a97191SGreg Kroah-Hartman kfree(msgbody);
44546a97191SGreg Kroah-Hartman return -ENOMEM;
44646a97191SGreg Kroah-Hartman }
44746a97191SGreg Kroah-Hartman
44846a97191SGreg Kroah-Hartman /*
449c1135c7fSBoqun Feng * __vmbus_establish_gpadl - Establish a GPADL for a buffer or ringbuffer
45046a97191SGreg Kroah-Hartman *
45146a97191SGreg Kroah-Hartman * @channel: a channel
452c1135c7fSBoqun Feng * @type: the type of the corresponding GPADL, only meaningful for the guest.
453b679ef73SHaiyang Zhang * @kbuffer: from kmalloc or vmalloc
45446a97191SGreg Kroah-Hartman * @size: page-size multiple
455c1135c7fSBoqun Feng * @send_offset: the offset (in bytes) where the send ring buffer starts,
456c1135c7fSBoqun Feng * should be 0 for BUFFER type gpadl
45746a97191SGreg Kroah-Hartman * @gpadl_handle: some funky thing
45846a97191SGreg Kroah-Hartman */
__vmbus_establish_gpadl(struct vmbus_channel * channel,enum hv_gpadl_type type,void * kbuffer,u32 size,u32 send_offset,struct vmbus_gpadl * gpadl)459c1135c7fSBoqun Feng static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
460c1135c7fSBoqun Feng enum hv_gpadl_type type, void *kbuffer,
461c1135c7fSBoqun Feng u32 size, u32 send_offset,
462d4dccf35STianyu Lan struct vmbus_gpadl *gpadl)
46346a97191SGreg Kroah-Hartman {
46446a97191SGreg Kroah-Hartman struct vmbus_channel_gpadl_header *gpadlmsg;
46546a97191SGreg Kroah-Hartman struct vmbus_channel_gpadl_body *gpadl_body;
46646a97191SGreg Kroah-Hartman struct vmbus_channel_msginfo *msginfo = NULL;
4677cc80c98SVitaly Kuznetsov struct vmbus_channel_msginfo *submsginfo, *tmp;
46846a97191SGreg Kroah-Hartman struct list_head *curr;
46946a97191SGreg Kroah-Hartman u32 next_gpadl_handle;
47046a97191SGreg Kroah-Hartman unsigned long flags;
47146a97191SGreg Kroah-Hartman int ret = 0;
47246a97191SGreg Kroah-Hartman
4739f52a163SK. Y. Srinivasan next_gpadl_handle =
4749f52a163SK. Y. Srinivasan (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
47546a97191SGreg Kroah-Hartman
476c1135c7fSBoqun Feng ret = create_gpadl_header(type, kbuffer, size, send_offset, &msginfo);
4778e62341fSRick Edgecombe if (ret) {
4788e62341fSRick Edgecombe gpadl->decrypted = false;
47946a97191SGreg Kroah-Hartman return ret;
4808e62341fSRick Edgecombe }
48146a97191SGreg Kroah-Hartman
4828e62341fSRick Edgecombe /*
4838e62341fSRick Edgecombe * Set the "decrypted" flag to true for the set_memory_decrypted()
4848e62341fSRick Edgecombe * success case. In the failure case, the encryption state of the
4858e62341fSRick Edgecombe * memory is unknown. Leave "decrypted" as true to ensure the
4868e62341fSRick Edgecombe * memory will be leaked instead of going back on the free list.
4878e62341fSRick Edgecombe */
4888e62341fSRick Edgecombe gpadl->decrypted = true;
489d4dccf35STianyu Lan ret = set_memory_decrypted((unsigned long)kbuffer,
490d4dccf35STianyu Lan PFN_UP(size));
491d4dccf35STianyu Lan if (ret) {
492d4dccf35STianyu Lan dev_warn(&channel->device_obj->device,
493d4dccf35STianyu Lan "Failed to set host visibility for new GPADL %d.\n",
494d4dccf35STianyu Lan ret);
495d4dccf35STianyu Lan return ret;
496d4dccf35STianyu Lan }
497d4dccf35STianyu Lan
49846a97191SGreg Kroah-Hartman init_completion(&msginfo->waitevent);
499ccb61f8aSK. Y. Srinivasan msginfo->waiting_channel = channel;
50046a97191SGreg Kroah-Hartman
50146a97191SGreg Kroah-Hartman gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
50246a97191SGreg Kroah-Hartman gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
50346a97191SGreg Kroah-Hartman gpadlmsg->child_relid = channel->offermsg.child_relid;
50446a97191SGreg Kroah-Hartman gpadlmsg->gpadl = next_gpadl_handle;
50546a97191SGreg Kroah-Hartman
50646a97191SGreg Kroah-Hartman
50746a97191SGreg Kroah-Hartman spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
50846a97191SGreg Kroah-Hartman list_add_tail(&msginfo->msglistentry,
50946a97191SGreg Kroah-Hartman &vmbus_connection.chn_msg_list);
51046a97191SGreg Kroah-Hartman
51146a97191SGreg Kroah-Hartman spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
51246a97191SGreg Kroah-Hartman
5136f3d791fSK. Y. Srinivasan if (channel->rescind) {
5146f3d791fSK. Y. Srinivasan ret = -ENODEV;
5156f3d791fSK. Y. Srinivasan goto cleanup;
5166f3d791fSK. Y. Srinivasan }
5176f3d791fSK. Y. Srinivasan
51846a97191SGreg Kroah-Hartman ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
519c0bb0392SVitaly Kuznetsov sizeof(*msginfo), true);
52069edbd5fSVitaly Kuznetsov
52169edbd5fSVitaly Kuznetsov trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
52269edbd5fSVitaly Kuznetsov
52346a97191SGreg Kroah-Hartman if (ret != 0)
52446a97191SGreg Kroah-Hartman goto cleanup;
52546a97191SGreg Kroah-Hartman
52646a97191SGreg Kroah-Hartman list_for_each(curr, &msginfo->submsglist) {
52746a97191SGreg Kroah-Hartman submsginfo = (struct vmbus_channel_msginfo *)curr;
52846a97191SGreg Kroah-Hartman gpadl_body =
52946a97191SGreg Kroah-Hartman (struct vmbus_channel_gpadl_body *)submsginfo->msg;
53046a97191SGreg Kroah-Hartman
53146a97191SGreg Kroah-Hartman gpadl_body->header.msgtype =
53246a97191SGreg Kroah-Hartman CHANNELMSG_GPADL_BODY;
53346a97191SGreg Kroah-Hartman gpadl_body->gpadl = next_gpadl_handle;
53446a97191SGreg Kroah-Hartman
53546a97191SGreg Kroah-Hartman ret = vmbus_post_msg(gpadl_body,
536c0bb0392SVitaly Kuznetsov submsginfo->msgsize - sizeof(*submsginfo),
537c0bb0392SVitaly Kuznetsov true);
53869edbd5fSVitaly Kuznetsov
53969edbd5fSVitaly Kuznetsov trace_vmbus_establish_gpadl_body(gpadl_body, ret);
54069edbd5fSVitaly Kuznetsov
54146a97191SGreg Kroah-Hartman if (ret != 0)
54246a97191SGreg Kroah-Hartman goto cleanup;
54346a97191SGreg Kroah-Hartman
54446a97191SGreg Kroah-Hartman }
54572c6b71cSK. Y. Srinivasan wait_for_completion(&msginfo->waitevent);
54646a97191SGreg Kroah-Hartman
547eceb0596SDexuan Cui if (msginfo->response.gpadl_created.creation_status != 0) {
548eceb0596SDexuan Cui pr_err("Failed to establish GPADL: err = 0x%x\n",
549eceb0596SDexuan Cui msginfo->response.gpadl_created.creation_status);
550eceb0596SDexuan Cui
551eceb0596SDexuan Cui ret = -EDQUOT;
552eceb0596SDexuan Cui goto cleanup;
553eceb0596SDexuan Cui }
554eceb0596SDexuan Cui
555ccb61f8aSK. Y. Srinivasan if (channel->rescind) {
556ccb61f8aSK. Y. Srinivasan ret = -ENODEV;
557ccb61f8aSK. Y. Srinivasan goto cleanup;
558ccb61f8aSK. Y. Srinivasan }
559ccb61f8aSK. Y. Srinivasan
56046a97191SGreg Kroah-Hartman /* At this point, we received the gpadl created msg */
561d4dccf35STianyu Lan gpadl->gpadl_handle = gpadlmsg->gpadl;
562d4dccf35STianyu Lan gpadl->buffer = kbuffer;
563d4dccf35STianyu Lan gpadl->size = size;
564d4dccf35STianyu Lan
56546a97191SGreg Kroah-Hartman
56646a97191SGreg Kroah-Hartman cleanup:
56746a97191SGreg Kroah-Hartman spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
56846a97191SGreg Kroah-Hartman list_del(&msginfo->msglistentry);
56946a97191SGreg Kroah-Hartman spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
5707cc80c98SVitaly Kuznetsov list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
5717cc80c98SVitaly Kuznetsov msglistentry) {
5727cc80c98SVitaly Kuznetsov kfree(submsginfo);
5737cc80c98SVitaly Kuznetsov }
57446a97191SGreg Kroah-Hartman
57546a97191SGreg Kroah-Hartman kfree(msginfo);
576d4dccf35STianyu Lan
5778e62341fSRick Edgecombe if (ret) {
5788e62341fSRick Edgecombe /*
5798e62341fSRick Edgecombe * If set_memory_encrypted() fails, the decrypted flag is
5808e62341fSRick Edgecombe * left as true so the memory is leaked instead of being
5818e62341fSRick Edgecombe * put back on the free list.
5828e62341fSRick Edgecombe */
5838e62341fSRick Edgecombe if (!set_memory_encrypted((unsigned long)kbuffer, PFN_UP(size)))
5848e62341fSRick Edgecombe gpadl->decrypted = false;
5858e62341fSRick Edgecombe }
586d4dccf35STianyu Lan
58746a97191SGreg Kroah-Hartman return ret;
58846a97191SGreg Kroah-Hartman }
589c1135c7fSBoqun Feng
590c1135c7fSBoqun Feng /*
591c1135c7fSBoqun Feng * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
592c1135c7fSBoqun Feng *
593c1135c7fSBoqun Feng * @channel: a channel
594c1135c7fSBoqun Feng * @kbuffer: from kmalloc or vmalloc
595c1135c7fSBoqun Feng * @size: page-size multiple
596c1135c7fSBoqun Feng * @gpadl_handle: some funky thing
597c1135c7fSBoqun Feng */
vmbus_establish_gpadl(struct vmbus_channel * channel,void * kbuffer,u32 size,struct vmbus_gpadl * gpadl)598c1135c7fSBoqun Feng int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
599d4dccf35STianyu Lan u32 size, struct vmbus_gpadl *gpadl)
600c1135c7fSBoqun Feng {
601c1135c7fSBoqun Feng return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER, kbuffer, size,
602d4dccf35STianyu Lan 0U, gpadl);
603c1135c7fSBoqun Feng }
60446a97191SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
60546a97191SGreg Kroah-Hartman
606e8b7db38SAndres Beltran /**
607e8b7db38SAndres Beltran * request_arr_init - Allocates memory for the requestor array. Each slot
608e8b7db38SAndres Beltran * keeps track of the next available slot in the array. Initially, each
609e8b7db38SAndres Beltran * slot points to the next one (as in a Linked List). The last slot
610e8b7db38SAndres Beltran * does not point to anything, so its value is U64_MAX by default.
611e8b7db38SAndres Beltran * @size The size of the array
612e8b7db38SAndres Beltran */
request_arr_init(u32 size)613e8b7db38SAndres Beltran static u64 *request_arr_init(u32 size)
614e8b7db38SAndres Beltran {
615e8b7db38SAndres Beltran int i;
616e8b7db38SAndres Beltran u64 *req_arr;
617e8b7db38SAndres Beltran
618e8b7db38SAndres Beltran req_arr = kcalloc(size, sizeof(u64), GFP_KERNEL);
619e8b7db38SAndres Beltran if (!req_arr)
620e8b7db38SAndres Beltran return NULL;
621e8b7db38SAndres Beltran
622e8b7db38SAndres Beltran for (i = 0; i < size - 1; i++)
623e8b7db38SAndres Beltran req_arr[i] = i + 1;
624e8b7db38SAndres Beltran
625e8b7db38SAndres Beltran /* Last slot (no more available slots) */
626e8b7db38SAndres Beltran req_arr[i] = U64_MAX;
627e8b7db38SAndres Beltran
628e8b7db38SAndres Beltran return req_arr;
629e8b7db38SAndres Beltran }
630e8b7db38SAndres Beltran
631e8b7db38SAndres Beltran /*
632e8b7db38SAndres Beltran * vmbus_alloc_requestor - Initializes @rqstor's fields.
633e8b7db38SAndres Beltran * Index 0 is the first free slot
634e8b7db38SAndres Beltran * @size: Size of the requestor array
635e8b7db38SAndres Beltran */
vmbus_alloc_requestor(struct vmbus_requestor * rqstor,u32 size)636e8b7db38SAndres Beltran static int vmbus_alloc_requestor(struct vmbus_requestor *rqstor, u32 size)
637e8b7db38SAndres Beltran {
638e8b7db38SAndres Beltran u64 *rqst_arr;
639e8b7db38SAndres Beltran unsigned long *bitmap;
640e8b7db38SAndres Beltran
641e8b7db38SAndres Beltran rqst_arr = request_arr_init(size);
642e8b7db38SAndres Beltran if (!rqst_arr)
643e8b7db38SAndres Beltran return -ENOMEM;
644e8b7db38SAndres Beltran
645e8b7db38SAndres Beltran bitmap = bitmap_zalloc(size, GFP_KERNEL);
646e8b7db38SAndres Beltran if (!bitmap) {
647e8b7db38SAndres Beltran kfree(rqst_arr);
648e8b7db38SAndres Beltran return -ENOMEM;
649e8b7db38SAndres Beltran }
650e8b7db38SAndres Beltran
651e8b7db38SAndres Beltran rqstor->req_arr = rqst_arr;
652e8b7db38SAndres Beltran rqstor->req_bitmap = bitmap;
653e8b7db38SAndres Beltran rqstor->size = size;
654e8b7db38SAndres Beltran rqstor->next_request_id = 0;
655e8b7db38SAndres Beltran spin_lock_init(&rqstor->req_lock);
656e8b7db38SAndres Beltran
657e8b7db38SAndres Beltran return 0;
658e8b7db38SAndres Beltran }
659e8b7db38SAndres Beltran
660e8b7db38SAndres Beltran /*
661e8b7db38SAndres Beltran * vmbus_free_requestor - Frees memory allocated for @rqstor
662e8b7db38SAndres Beltran * @rqstor: Pointer to the requestor struct
663e8b7db38SAndres Beltran */
vmbus_free_requestor(struct vmbus_requestor * rqstor)664e8b7db38SAndres Beltran static void vmbus_free_requestor(struct vmbus_requestor *rqstor)
665e8b7db38SAndres Beltran {
666e8b7db38SAndres Beltran kfree(rqstor->req_arr);
667e8b7db38SAndres Beltran bitmap_free(rqstor->req_bitmap);
668e8b7db38SAndres Beltran }
669e8b7db38SAndres Beltran
__vmbus_open(struct vmbus_channel * newchannel,void * userdata,u32 userdatalen,void (* onchannelcallback)(void * context),void * context)670edd9bbc1SBoqun Feng static int __vmbus_open(struct vmbus_channel *newchannel,
671edd9bbc1SBoqun Feng void *userdata, u32 userdatalen,
672edd9bbc1SBoqun Feng void (*onchannelcallback)(void *context), void *context)
673edd9bbc1SBoqun Feng {
674edd9bbc1SBoqun Feng struct vmbus_channel_open_channel *open_msg;
675edd9bbc1SBoqun Feng struct vmbus_channel_msginfo *open_info = NULL;
676edd9bbc1SBoqun Feng struct page *page = newchannel->ringbuffer_page;
677edd9bbc1SBoqun Feng u32 send_pages, recv_pages;
678edd9bbc1SBoqun Feng unsigned long flags;
679edd9bbc1SBoqun Feng int err;
680edd9bbc1SBoqun Feng
681edd9bbc1SBoqun Feng if (userdatalen > MAX_USER_DEFINED_BYTES)
682edd9bbc1SBoqun Feng return -EINVAL;
683edd9bbc1SBoqun Feng
684edd9bbc1SBoqun Feng send_pages = newchannel->ringbuffer_send_offset;
685edd9bbc1SBoqun Feng recv_pages = newchannel->ringbuffer_pagecount - send_pages;
686edd9bbc1SBoqun Feng
687edd9bbc1SBoqun Feng if (newchannel->state != CHANNEL_OPEN_STATE)
688edd9bbc1SBoqun Feng return -EINVAL;
689edd9bbc1SBoqun Feng
690e8b7db38SAndres Beltran /* Create and init requestor */
691e8b7db38SAndres Beltran if (newchannel->rqstor_size) {
692e8b7db38SAndres Beltran if (vmbus_alloc_requestor(&newchannel->requestor, newchannel->rqstor_size))
693e8b7db38SAndres Beltran return -ENOMEM;
694e8b7db38SAndres Beltran }
695e8b7db38SAndres Beltran
696edd9bbc1SBoqun Feng newchannel->state = CHANNEL_OPENING_STATE;
697edd9bbc1SBoqun Feng newchannel->onchannel_callback = onchannelcallback;
698edd9bbc1SBoqun Feng newchannel->channel_callback_context = context;
699edd9bbc1SBoqun Feng
700adae1e93SAndres Beltran if (!newchannel->max_pkt_size)
701adae1e93SAndres Beltran newchannel->max_pkt_size = VMBUS_DEFAULT_MAX_PKT_SIZE;
702adae1e93SAndres Beltran
703edd9bbc1SBoqun Feng /* Establish the gpadl for the ring buffer */
704d4dccf35STianyu Lan newchannel->ringbuffer_gpadlhandle.gpadl_handle = 0;
705edd9bbc1SBoqun Feng
706c1135c7fSBoqun Feng err = __vmbus_establish_gpadl(newchannel, HV_GPADL_RING,
707edd9bbc1SBoqun Feng page_address(newchannel->ringbuffer_page),
708edd9bbc1SBoqun Feng (send_pages + recv_pages) << PAGE_SHIFT,
709c1135c7fSBoqun Feng newchannel->ringbuffer_send_offset << PAGE_SHIFT,
710edd9bbc1SBoqun Feng &newchannel->ringbuffer_gpadlhandle);
711edd9bbc1SBoqun Feng if (err)
712edd9bbc1SBoqun Feng goto error_clean_ring;
713edd9bbc1SBoqun Feng
7149a879772STianyu Lan err = hv_ringbuffer_init(&newchannel->outbound,
7159a879772STianyu Lan page, send_pages, 0);
7169a879772STianyu Lan if (err)
7179a879772STianyu Lan goto error_free_gpadl;
7189a879772STianyu Lan
7199a879772STianyu Lan err = hv_ringbuffer_init(&newchannel->inbound, &page[send_pages],
7209a879772STianyu Lan recv_pages, newchannel->max_pkt_size);
7219a879772STianyu Lan if (err)
7229a879772STianyu Lan goto error_free_gpadl;
7239a879772STianyu Lan
724edd9bbc1SBoqun Feng /* Create and init the channel open message */
725e99c4afbSAndrea Parri (Microsoft) open_info = kzalloc(sizeof(*open_info) +
726edd9bbc1SBoqun Feng sizeof(struct vmbus_channel_open_channel),
727edd9bbc1SBoqun Feng GFP_KERNEL);
728edd9bbc1SBoqun Feng if (!open_info) {
729edd9bbc1SBoqun Feng err = -ENOMEM;
730edd9bbc1SBoqun Feng goto error_free_gpadl;
731edd9bbc1SBoqun Feng }
732edd9bbc1SBoqun Feng
733edd9bbc1SBoqun Feng init_completion(&open_info->waitevent);
734edd9bbc1SBoqun Feng open_info->waiting_channel = newchannel;
735edd9bbc1SBoqun Feng
736edd9bbc1SBoqun Feng open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
737edd9bbc1SBoqun Feng open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
738edd9bbc1SBoqun Feng open_msg->openid = newchannel->offermsg.child_relid;
739edd9bbc1SBoqun Feng open_msg->child_relid = newchannel->offermsg.child_relid;
740d4dccf35STianyu Lan open_msg->ringbuffer_gpadlhandle
741d4dccf35STianyu Lan = newchannel->ringbuffer_gpadlhandle.gpadl_handle;
742c1135c7fSBoqun Feng /*
743c1135c7fSBoqun Feng * The unit of ->downstream_ringbuffer_pageoffset is HV_HYP_PAGE and
744c1135c7fSBoqun Feng * the unit of ->ringbuffer_send_offset (i.e. send_pages) is PAGE, so
745c1135c7fSBoqun Feng * here we calculate it into HV_HYP_PAGE.
746c1135c7fSBoqun Feng */
747c1135c7fSBoqun Feng open_msg->downstream_ringbuffer_pageoffset =
748c1135c7fSBoqun Feng hv_ring_gpadl_send_hvpgoffset(send_pages << PAGE_SHIFT);
749edd9bbc1SBoqun Feng open_msg->target_vp = hv_cpu_number_to_vp_number(newchannel->target_cpu);
750edd9bbc1SBoqun Feng
751edd9bbc1SBoqun Feng if (userdatalen)
752edd9bbc1SBoqun Feng memcpy(open_msg->userdata, userdata, userdatalen);
753edd9bbc1SBoqun Feng
754edd9bbc1SBoqun Feng spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
755edd9bbc1SBoqun Feng list_add_tail(&open_info->msglistentry,
756edd9bbc1SBoqun Feng &vmbus_connection.chn_msg_list);
757edd9bbc1SBoqun Feng spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
758edd9bbc1SBoqun Feng
759edd9bbc1SBoqun Feng if (newchannel->rescind) {
760edd9bbc1SBoqun Feng err = -ENODEV;
7613e9bf43fSDan Carpenter goto error_clean_msglist;
762edd9bbc1SBoqun Feng }
763edd9bbc1SBoqun Feng
764edd9bbc1SBoqun Feng err = vmbus_post_msg(open_msg,
765edd9bbc1SBoqun Feng sizeof(struct vmbus_channel_open_channel), true);
766edd9bbc1SBoqun Feng
767edd9bbc1SBoqun Feng trace_vmbus_open(open_msg, err);
768edd9bbc1SBoqun Feng
769edd9bbc1SBoqun Feng if (err != 0)
770edd9bbc1SBoqun Feng goto error_clean_msglist;
771edd9bbc1SBoqun Feng
772edd9bbc1SBoqun Feng wait_for_completion(&open_info->waitevent);
773edd9bbc1SBoqun Feng
774edd9bbc1SBoqun Feng spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
775edd9bbc1SBoqun Feng list_del(&open_info->msglistentry);
776edd9bbc1SBoqun Feng spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
777edd9bbc1SBoqun Feng
778edd9bbc1SBoqun Feng if (newchannel->rescind) {
779edd9bbc1SBoqun Feng err = -ENODEV;
780edd9bbc1SBoqun Feng goto error_free_info;
781edd9bbc1SBoqun Feng }
782edd9bbc1SBoqun Feng
783edd9bbc1SBoqun Feng if (open_info->response.open_result.status) {
784edd9bbc1SBoqun Feng err = -EAGAIN;
785edd9bbc1SBoqun Feng goto error_free_info;
786edd9bbc1SBoqun Feng }
787edd9bbc1SBoqun Feng
788edd9bbc1SBoqun Feng newchannel->state = CHANNEL_OPENED_STATE;
789edd9bbc1SBoqun Feng kfree(open_info);
790edd9bbc1SBoqun Feng return 0;
791edd9bbc1SBoqun Feng
792edd9bbc1SBoqun Feng error_clean_msglist:
793edd9bbc1SBoqun Feng spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
794edd9bbc1SBoqun Feng list_del(&open_info->msglistentry);
795edd9bbc1SBoqun Feng spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
796edd9bbc1SBoqun Feng error_free_info:
797edd9bbc1SBoqun Feng kfree(open_info);
798edd9bbc1SBoqun Feng error_free_gpadl:
799d4dccf35STianyu Lan vmbus_teardown_gpadl(newchannel, &newchannel->ringbuffer_gpadlhandle);
800edd9bbc1SBoqun Feng error_clean_ring:
801edd9bbc1SBoqun Feng hv_ringbuffer_cleanup(&newchannel->outbound);
802edd9bbc1SBoqun Feng hv_ringbuffer_cleanup(&newchannel->inbound);
803e8b7db38SAndres Beltran vmbus_free_requestor(&newchannel->requestor);
804edd9bbc1SBoqun Feng newchannel->state = CHANNEL_OPEN_STATE;
805edd9bbc1SBoqun Feng return err;
806edd9bbc1SBoqun Feng }
807edd9bbc1SBoqun Feng
808edd9bbc1SBoqun Feng /*
809edd9bbc1SBoqun Feng * vmbus_connect_ring - Open the channel but reuse ring buffer
810edd9bbc1SBoqun Feng */
vmbus_connect_ring(struct vmbus_channel * newchannel,void (* onchannelcallback)(void * context),void * context)811edd9bbc1SBoqun Feng int vmbus_connect_ring(struct vmbus_channel *newchannel,
812edd9bbc1SBoqun Feng void (*onchannelcallback)(void *context), void *context)
813edd9bbc1SBoqun Feng {
814edd9bbc1SBoqun Feng return __vmbus_open(newchannel, NULL, 0, onchannelcallback, context);
815edd9bbc1SBoqun Feng }
816edd9bbc1SBoqun Feng EXPORT_SYMBOL_GPL(vmbus_connect_ring);
817edd9bbc1SBoqun Feng
818edd9bbc1SBoqun Feng /*
819edd9bbc1SBoqun Feng * vmbus_open - Open the specified channel.
820edd9bbc1SBoqun Feng */
vmbus_open(struct vmbus_channel * newchannel,u32 send_ringbuffer_size,u32 recv_ringbuffer_size,void * userdata,u32 userdatalen,void (* onchannelcallback)(void * context),void * context)821edd9bbc1SBoqun Feng int vmbus_open(struct vmbus_channel *newchannel,
822edd9bbc1SBoqun Feng u32 send_ringbuffer_size, u32 recv_ringbuffer_size,
823edd9bbc1SBoqun Feng void *userdata, u32 userdatalen,
824edd9bbc1SBoqun Feng void (*onchannelcallback)(void *context), void *context)
825edd9bbc1SBoqun Feng {
826edd9bbc1SBoqun Feng int err;
827edd9bbc1SBoqun Feng
828edd9bbc1SBoqun Feng err = vmbus_alloc_ring(newchannel, send_ringbuffer_size,
829edd9bbc1SBoqun Feng recv_ringbuffer_size);
830edd9bbc1SBoqun Feng if (err)
831edd9bbc1SBoqun Feng return err;
832edd9bbc1SBoqun Feng
833edd9bbc1SBoqun Feng err = __vmbus_open(newchannel, userdata, userdatalen,
834edd9bbc1SBoqun Feng onchannelcallback, context);
835edd9bbc1SBoqun Feng if (err)
836edd9bbc1SBoqun Feng vmbus_free_ring(newchannel);
837edd9bbc1SBoqun Feng
838edd9bbc1SBoqun Feng return err;
839edd9bbc1SBoqun Feng }
840edd9bbc1SBoqun Feng EXPORT_SYMBOL_GPL(vmbus_open);
841edd9bbc1SBoqun Feng
84246a97191SGreg Kroah-Hartman /*
84346a97191SGreg Kroah-Hartman * vmbus_teardown_gpadl -Teardown the specified GPADL handle
84446a97191SGreg Kroah-Hartman */
vmbus_teardown_gpadl(struct vmbus_channel * channel,struct vmbus_gpadl * gpadl)845d4dccf35STianyu Lan int vmbus_teardown_gpadl(struct vmbus_channel *channel, struct vmbus_gpadl *gpadl)
84646a97191SGreg Kroah-Hartman {
84746a97191SGreg Kroah-Hartman struct vmbus_channel_gpadl_teardown *msg;
84846a97191SGreg Kroah-Hartman struct vmbus_channel_msginfo *info;
84946a97191SGreg Kroah-Hartman unsigned long flags;
85066be6530SK. Y. Srinivasan int ret;
85146a97191SGreg Kroah-Hartman
852e99c4afbSAndrea Parri (Microsoft) info = kzalloc(sizeof(*info) +
85346a97191SGreg Kroah-Hartman sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
85446a97191SGreg Kroah-Hartman if (!info)
85546a97191SGreg Kroah-Hartman return -ENOMEM;
85646a97191SGreg Kroah-Hartman
85746a97191SGreg Kroah-Hartman init_completion(&info->waitevent);
858ccb61f8aSK. Y. Srinivasan info->waiting_channel = channel;
85946a97191SGreg Kroah-Hartman
86046a97191SGreg Kroah-Hartman msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
86146a97191SGreg Kroah-Hartman
86246a97191SGreg Kroah-Hartman msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
86346a97191SGreg Kroah-Hartman msg->child_relid = channel->offermsg.child_relid;
864d4dccf35STianyu Lan msg->gpadl = gpadl->gpadl_handle;
86546a97191SGreg Kroah-Hartman
86646a97191SGreg Kroah-Hartman spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
86746a97191SGreg Kroah-Hartman list_add_tail(&info->msglistentry,
86846a97191SGreg Kroah-Hartman &vmbus_connection.chn_msg_list);
86946a97191SGreg Kroah-Hartman spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
8706f3d791fSK. Y. Srinivasan
8716f3d791fSK. Y. Srinivasan if (channel->rescind)
8726f3d791fSK. Y. Srinivasan goto post_msg_err;
8736f3d791fSK. Y. Srinivasan
874c0bb0392SVitaly Kuznetsov ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
875c0bb0392SVitaly Kuznetsov true);
87646a97191SGreg Kroah-Hartman
87709cdf8f8SVitaly Kuznetsov trace_vmbus_teardown_gpadl(msg, ret);
87809cdf8f8SVitaly Kuznetsov
87966be6530SK. Y. Srinivasan if (ret)
88066be6530SK. Y. Srinivasan goto post_msg_err;
88146a97191SGreg Kroah-Hartman
88266be6530SK. Y. Srinivasan wait_for_completion(&info->waitevent);
88366be6530SK. Y. Srinivasan
884d4dccf35STianyu Lan gpadl->gpadl_handle = 0;
885d4dccf35STianyu Lan
88666be6530SK. Y. Srinivasan post_msg_err:
8875e030d5cSK. Y. Srinivasan /*
8885e030d5cSK. Y. Srinivasan * If the channel has been rescinded;
8895e030d5cSK. Y. Srinivasan * we will be awakened by the rescind
8905e030d5cSK. Y. Srinivasan * handler; set the error code to zero so we don't leak memory.
8915e030d5cSK. Y. Srinivasan */
8925e030d5cSK. Y. Srinivasan if (channel->rescind)
8935e030d5cSK. Y. Srinivasan ret = 0;
8945e030d5cSK. Y. Srinivasan
89546a97191SGreg Kroah-Hartman spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
89646a97191SGreg Kroah-Hartman list_del(&info->msglistentry);
89746a97191SGreg Kroah-Hartman spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
89846a97191SGreg Kroah-Hartman
89946a97191SGreg Kroah-Hartman kfree(info);
900d4dccf35STianyu Lan
901d4dccf35STianyu Lan ret = set_memory_encrypted((unsigned long)gpadl->buffer,
902d4dccf35STianyu Lan PFN_UP(gpadl->size));
903d4dccf35STianyu Lan if (ret)
904d4dccf35STianyu Lan pr_warn("Fail to set mem host visibility in GPADL teardown %d.\n", ret);
905d4dccf35STianyu Lan
9068e62341fSRick Edgecombe gpadl->decrypted = ret;
9078e62341fSRick Edgecombe
90846a97191SGreg Kroah-Hartman return ret;
90946a97191SGreg Kroah-Hartman }
91046a97191SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
91146a97191SGreg Kroah-Hartman
vmbus_reset_channel_cb(struct vmbus_channel * channel)912d3b26dd7SDexuan Cui void vmbus_reset_channel_cb(struct vmbus_channel *channel)
91346a97191SGreg Kroah-Hartman {
9149403b66eSAndrea Parri (Microsoft) unsigned long flags;
9159403b66eSAndrea Parri (Microsoft)
91663d55b2aSDexuan Cui /*
917dad72a1dSDexuan Cui * vmbus_on_event(), running in the per-channel tasklet, can race
91863d55b2aSDexuan Cui * with vmbus_close_internal() in the case of SMP guest, e.g., when
91963d55b2aSDexuan Cui * the former is accessing channel->inbound.ring_buffer, the latter
920dad72a1dSDexuan Cui * could be freeing the ring_buffer pages, so here we must stop it
921dad72a1dSDexuan Cui * first.
922ac504767SAndrea Parri (Microsoft) *
923ac504767SAndrea Parri (Microsoft) * vmbus_chan_sched() might call the netvsc driver callback function
924ac504767SAndrea Parri (Microsoft) * that ends up scheduling NAPI work that accesses the ring buffer.
925ac504767SAndrea Parri (Microsoft) * At this point, we have to ensure that any such work is completed
926ac504767SAndrea Parri (Microsoft) * and that the channel ring buffer is no longer being accessed, cf.
927ac504767SAndrea Parri (Microsoft) * the calls to napi_disable() in netvsc_device_remove().
92863d55b2aSDexuan Cui */
929dad72a1dSDexuan Cui tasklet_disable(&channel->callback_event);
93063d55b2aSDexuan Cui
9319403b66eSAndrea Parri (Microsoft) /* See the inline comments in vmbus_chan_sched(). */
9329403b66eSAndrea Parri (Microsoft) spin_lock_irqsave(&channel->sched_lock, flags);
9339403b66eSAndrea Parri (Microsoft) channel->onchannel_callback = NULL;
9349403b66eSAndrea Parri (Microsoft) spin_unlock_irqrestore(&channel->sched_lock, flags);
935d3b26dd7SDexuan Cui
9369403b66eSAndrea Parri (Microsoft) channel->sc_creation_callback = NULL;
937d3b26dd7SDexuan Cui
938d3b26dd7SDexuan Cui /* Re-enable tasklet for use on re-open */
939d3b26dd7SDexuan Cui tasklet_enable(&channel->callback_event);
940d3b26dd7SDexuan Cui }
941d3b26dd7SDexuan Cui
vmbus_close_internal(struct vmbus_channel * channel)942d3b26dd7SDexuan Cui static int vmbus_close_internal(struct vmbus_channel *channel)
943d3b26dd7SDexuan Cui {
944d3b26dd7SDexuan Cui struct vmbus_channel_close_channel *msg;
945d3b26dd7SDexuan Cui int ret;
946d3b26dd7SDexuan Cui
947d3b26dd7SDexuan Cui vmbus_reset_channel_cb(channel);
948d3b26dd7SDexuan Cui
94964b7faf9SDexuan Cui /*
95064b7faf9SDexuan Cui * In case a device driver's probe() fails (e.g.,
95164b7faf9SDexuan Cui * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
9528a1115ffSMasahiro Yamada * rescinded later (e.g., we dynamically disable an Integrated Service
95364b7faf9SDexuan Cui * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
95464b7faf9SDexuan Cui * here we should skip most of the below cleanup work.
95564b7faf9SDexuan Cui */
956ae6935edSStephen Hemminger if (channel->state != CHANNEL_OPENED_STATE)
957ae6935edSStephen Hemminger return -EINVAL;
95864b7faf9SDexuan Cui
959e68d2971SK. Y. Srinivasan channel->state = CHANNEL_OPEN_STATE;
96046a97191SGreg Kroah-Hartman
96146a97191SGreg Kroah-Hartman /* Send a closing message */
96246a97191SGreg Kroah-Hartman
96346a97191SGreg Kroah-Hartman msg = &channel->close_msg.msg;
96446a97191SGreg Kroah-Hartman
96546a97191SGreg Kroah-Hartman msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
96646a97191SGreg Kroah-Hartman msg->child_relid = channel->offermsg.child_relid;
96746a97191SGreg Kroah-Hartman
968c0bb0392SVitaly Kuznetsov ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
969c0bb0392SVitaly Kuznetsov true);
97046a97191SGreg Kroah-Hartman
971633b005dSVitaly Kuznetsov trace_vmbus_close_internal(msg, ret);
972633b005dSVitaly Kuznetsov
97398d731bbSK. Y. Srinivasan if (ret) {
97498d731bbSK. Y. Srinivasan pr_err("Close failed: close post msg return is %d\n", ret);
97598d731bbSK. Y. Srinivasan /*
97698d731bbSK. Y. Srinivasan * If we failed to post the close msg,
97798d731bbSK. Y. Srinivasan * it is perhaps better to leak memory.
97898d731bbSK. Y. Srinivasan */
97998d731bbSK. Y. Srinivasan }
98098d731bbSK. Y. Srinivasan
98146a97191SGreg Kroah-Hartman /* Tear down the gpadl for the channel's ring buffer */
982d4dccf35STianyu Lan else if (channel->ringbuffer_gpadlhandle.gpadl_handle) {
983d4dccf35STianyu Lan ret = vmbus_teardown_gpadl(channel, &channel->ringbuffer_gpadlhandle);
98498d731bbSK. Y. Srinivasan if (ret) {
98598d731bbSK. Y. Srinivasan pr_err("Close failed: teardown gpadl return %d\n", ret);
98698d731bbSK. Y. Srinivasan /*
98798d731bbSK. Y. Srinivasan * If we failed to teardown gpadl,
98898d731bbSK. Y. Srinivasan * it is perhaps better to leak memory.
98998d731bbSK. Y. Srinivasan */
99098d731bbSK. Y. Srinivasan }
991ae6935edSStephen Hemminger }
99246a97191SGreg Kroah-Hartman
993e8b7db38SAndres Beltran if (!ret)
994e8b7db38SAndres Beltran vmbus_free_requestor(&channel->requestor);
995e8b7db38SAndres Beltran
99698d731bbSK. Y. Srinivasan return ret;
99746a97191SGreg Kroah-Hartman }
998e68d2971SK. Y. Srinivasan
999ae6935edSStephen Hemminger /* disconnect ring - close all channels */
vmbus_disconnect_ring(struct vmbus_channel * channel)1000ae6935edSStephen Hemminger int vmbus_disconnect_ring(struct vmbus_channel *channel)
1001ae6935edSStephen Hemminger {
1002ae6935edSStephen Hemminger struct vmbus_channel *cur_channel, *tmp;
1003ae6935edSStephen Hemminger int ret;
1004ae6935edSStephen Hemminger
1005ae6935edSStephen Hemminger if (channel->primary_channel != NULL)
1006ae6935edSStephen Hemminger return -EINVAL;
1007ae6935edSStephen Hemminger
1008b5679cebSDexuan Cui list_for_each_entry_safe(cur_channel, tmp, &channel->sc_list, sc_list) {
1009ae6935edSStephen Hemminger if (cur_channel->rescind)
1010ae6935edSStephen Hemminger wait_for_completion(&cur_channel->rescind_event);
1011ae6935edSStephen Hemminger
1012ae6935edSStephen Hemminger mutex_lock(&vmbus_connection.channel_mutex);
1013ae6935edSStephen Hemminger if (vmbus_close_internal(cur_channel) == 0) {
1014ae6935edSStephen Hemminger vmbus_free_ring(cur_channel);
1015ae6935edSStephen Hemminger
1016ae6935edSStephen Hemminger if (cur_channel->rescind)
1017ae6935edSStephen Hemminger hv_process_channel_removal(cur_channel);
1018ae6935edSStephen Hemminger }
1019ae6935edSStephen Hemminger mutex_unlock(&vmbus_connection.channel_mutex);
1020ae6935edSStephen Hemminger }
1021ae6935edSStephen Hemminger
1022ae6935edSStephen Hemminger /*
1023ae6935edSStephen Hemminger * Now close the primary.
1024ae6935edSStephen Hemminger */
1025ae6935edSStephen Hemminger mutex_lock(&vmbus_connection.channel_mutex);
1026ae6935edSStephen Hemminger ret = vmbus_close_internal(channel);
1027ae6935edSStephen Hemminger mutex_unlock(&vmbus_connection.channel_mutex);
1028ae6935edSStephen Hemminger
1029ae6935edSStephen Hemminger return ret;
1030ae6935edSStephen Hemminger }
1031ae6935edSStephen Hemminger EXPORT_SYMBOL_GPL(vmbus_disconnect_ring);
1032ae6935edSStephen Hemminger
1033e68d2971SK. Y. Srinivasan /*
1034e68d2971SK. Y. Srinivasan * vmbus_close - Close the specified channel
1035e68d2971SK. Y. Srinivasan */
vmbus_close(struct vmbus_channel * channel)1036e68d2971SK. Y. Srinivasan void vmbus_close(struct vmbus_channel *channel)
1037e68d2971SK. Y. Srinivasan {
1038ae6935edSStephen Hemminger if (vmbus_disconnect_ring(channel) == 0)
1039ae6935edSStephen Hemminger vmbus_free_ring(channel);
1040e68d2971SK. Y. Srinivasan }
104146a97191SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(vmbus_close);
104246a97191SGreg Kroah-Hartman
10435dd0fb9bSstephen hemminger /**
1044b03afa57SAndrea Parri (Microsoft) * vmbus_sendpacket_getid() - Send the specified buffer on the given channel
1045fe857bb4SDexuan Cui * @channel: Pointer to vmbus_channel structure
1046fe857bb4SDexuan Cui * @buffer: Pointer to the buffer you want to send the data from.
1047fe857bb4SDexuan Cui * @bufferlen: Maximum size of what the buffer holds.
10485dd0fb9bSstephen hemminger * @requestid: Identifier of the request
1049b03afa57SAndrea Parri (Microsoft) * @trans_id: Identifier of the transaction associated to this request, if
1050b03afa57SAndrea Parri (Microsoft) * the send is successful; undefined, otherwise.
1051fe857bb4SDexuan Cui * @type: Type of packet that is being sent e.g. negotiate, time
10525dd0fb9bSstephen hemminger * packet etc.
1053fe857bb4SDexuan Cui * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
10545dd0fb9bSstephen hemminger *
1055fe857bb4SDexuan Cui * Sends data in @buffer directly to Hyper-V via the vmbus.
1056fe857bb4SDexuan Cui * This will send the data unparsed to Hyper-V.
10575dd0fb9bSstephen hemminger *
10585dd0fb9bSstephen hemminger * Mainly used by Hyper-V drivers.
10595dd0fb9bSstephen hemminger */
vmbus_sendpacket_getid(struct vmbus_channel * channel,void * buffer,u32 bufferlen,u64 requestid,u64 * trans_id,enum vmbus_packet_type type,u32 flags)1060b03afa57SAndrea Parri (Microsoft) int vmbus_sendpacket_getid(struct vmbus_channel *channel, void *buffer,
1061b03afa57SAndrea Parri (Microsoft) u32 bufferlen, u64 requestid, u64 *trans_id,
10623454323cSStephen Hemminger enum vmbus_packet_type type, u32 flags)
106346a97191SGreg Kroah-Hartman {
106446a97191SGreg Kroah-Hartman struct vmpacket_descriptor desc;
106546a97191SGreg Kroah-Hartman u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
106646a97191SGreg Kroah-Hartman u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
1067011a7c3cSK. Y. Srinivasan struct kvec bufferlist[3];
106846a97191SGreg Kroah-Hartman u64 aligned_data = 0;
1069b81658cfSK. Y. Srinivasan int num_vecs = ((bufferlen != 0) ? 3 : 1);
107046a97191SGreg Kroah-Hartman
107146a97191SGreg Kroah-Hartman
107246a97191SGreg Kroah-Hartman /* Setup the descriptor */
107346a97191SGreg Kroah-Hartman desc.type = type; /* VmbusPacketTypeDataInBand; */
107446a97191SGreg Kroah-Hartman desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
107546a97191SGreg Kroah-Hartman /* in 8-bytes granularity */
107646a97191SGreg Kroah-Hartman desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
107746a97191SGreg Kroah-Hartman desc.len8 = (u16)(packetlen_aligned >> 3);
1078e8b7db38SAndres Beltran desc.trans_id = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
107946a97191SGreg Kroah-Hartman
1080011a7c3cSK. Y. Srinivasan bufferlist[0].iov_base = &desc;
1081011a7c3cSK. Y. Srinivasan bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
1082011a7c3cSK. Y. Srinivasan bufferlist[1].iov_base = buffer;
1083011a7c3cSK. Y. Srinivasan bufferlist[1].iov_len = bufferlen;
1084011a7c3cSK. Y. Srinivasan bufferlist[2].iov_base = &aligned_data;
1085011a7c3cSK. Y. Srinivasan bufferlist[2].iov_len = (packetlen_aligned - packetlen);
108646a97191SGreg Kroah-Hartman
1087b03afa57SAndrea Parri (Microsoft) return hv_ringbuffer_write(channel, bufferlist, num_vecs, requestid, trans_id);
1088b03afa57SAndrea Parri (Microsoft) }
1089b03afa57SAndrea Parri (Microsoft) EXPORT_SYMBOL(vmbus_sendpacket_getid);
1090b03afa57SAndrea Parri (Microsoft)
1091b03afa57SAndrea Parri (Microsoft) /**
1092b03afa57SAndrea Parri (Microsoft) * vmbus_sendpacket() - Send the specified buffer on the given channel
1093b03afa57SAndrea Parri (Microsoft) * @channel: Pointer to vmbus_channel structure
1094b03afa57SAndrea Parri (Microsoft) * @buffer: Pointer to the buffer you want to send the data from.
1095b03afa57SAndrea Parri (Microsoft) * @bufferlen: Maximum size of what the buffer holds.
1096b03afa57SAndrea Parri (Microsoft) * @requestid: Identifier of the request
1097b03afa57SAndrea Parri (Microsoft) * @type: Type of packet that is being sent e.g. negotiate, time
1098b03afa57SAndrea Parri (Microsoft) * packet etc.
1099b03afa57SAndrea Parri (Microsoft) * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
1100b03afa57SAndrea Parri (Microsoft) *
1101b03afa57SAndrea Parri (Microsoft) * Sends data in @buffer directly to Hyper-V via the vmbus.
1102b03afa57SAndrea Parri (Microsoft) * This will send the data unparsed to Hyper-V.
1103b03afa57SAndrea Parri (Microsoft) *
1104b03afa57SAndrea Parri (Microsoft) * Mainly used by Hyper-V drivers.
1105b03afa57SAndrea Parri (Microsoft) */
vmbus_sendpacket(struct vmbus_channel * channel,void * buffer,u32 bufferlen,u64 requestid,enum vmbus_packet_type type,u32 flags)1106b03afa57SAndrea Parri (Microsoft) int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
1107b03afa57SAndrea Parri (Microsoft) u32 bufferlen, u64 requestid,
1108b03afa57SAndrea Parri (Microsoft) enum vmbus_packet_type type, u32 flags)
1109b03afa57SAndrea Parri (Microsoft) {
1110b03afa57SAndrea Parri (Microsoft) return vmbus_sendpacket_getid(channel, buffer, bufferlen,
1111b03afa57SAndrea Parri (Microsoft) requestid, NULL, type, flags);
111246a97191SGreg Kroah-Hartman }
111346a97191SGreg Kroah-Hartman EXPORT_SYMBOL(vmbus_sendpacket);
111446a97191SGreg Kroah-Hartman
111546a97191SGreg Kroah-Hartman /*
11165a668d8cSstephen hemminger * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
111787e93d61SK. Y. Srinivasan * packets using a GPADL Direct packet type. This interface allows you
111887e93d61SK. Y. Srinivasan * to control notifying the host. This will be useful for sending
111987e93d61SK. Y. Srinivasan * batched data. Also the sender can control the send flags
112087e93d61SK. Y. Srinivasan * explicitly.
112146a97191SGreg Kroah-Hartman */
vmbus_sendpacket_pagebuffer(struct vmbus_channel * channel,struct hv_page_buffer pagebuffers[],u32 pagecount,void * buffer,u32 bufferlen,u64 requestid)11225a668d8cSstephen hemminger int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
112346a97191SGreg Kroah-Hartman struct hv_page_buffer pagebuffers[],
112446a97191SGreg Kroah-Hartman u32 pagecount, void *buffer, u32 bufferlen,
11255a668d8cSstephen hemminger u64 requestid)
112646a97191SGreg Kroah-Hartman {
112746a97191SGreg Kroah-Hartman int i;
112846a97191SGreg Kroah-Hartman struct vmbus_channel_packet_page_buffer desc;
112946a97191SGreg Kroah-Hartman u32 descsize;
113046a97191SGreg Kroah-Hartman u32 packetlen;
113146a97191SGreg Kroah-Hartman u32 packetlen_aligned;
1132011a7c3cSK. Y. Srinivasan struct kvec bufferlist[3];
113346a97191SGreg Kroah-Hartman u64 aligned_data = 0;
113446a97191SGreg Kroah-Hartman
113546a97191SGreg Kroah-Hartman if (pagecount > MAX_PAGE_BUFFER_COUNT)
113646a97191SGreg Kroah-Hartman return -EINVAL;
113746a97191SGreg Kroah-Hartman
113846a97191SGreg Kroah-Hartman /*
113946a97191SGreg Kroah-Hartman * Adjust the size down since vmbus_channel_packet_page_buffer is the
114046a97191SGreg Kroah-Hartman * largest size we support
114146a97191SGreg Kroah-Hartman */
114246a97191SGreg Kroah-Hartman descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
114346a97191SGreg Kroah-Hartman ((MAX_PAGE_BUFFER_COUNT - pagecount) *
114446a97191SGreg Kroah-Hartman sizeof(struct hv_page_buffer));
114546a97191SGreg Kroah-Hartman packetlen = descsize + bufferlen;
114646a97191SGreg Kroah-Hartman packetlen_aligned = ALIGN(packetlen, sizeof(u64));
114746a97191SGreg Kroah-Hartman
114846a97191SGreg Kroah-Hartman /* Setup the descriptor */
114946a97191SGreg Kroah-Hartman desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
11505a668d8cSstephen hemminger desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
1151bdc1dd47SStephen Hemminger desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
115246a97191SGreg Kroah-Hartman desc.length8 = (u16)(packetlen_aligned >> 3);
1153e8b7db38SAndres Beltran desc.transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
115433d426a9SStephen Hemminger desc.reserved = 0;
115546a97191SGreg Kroah-Hartman desc.rangecount = pagecount;
115646a97191SGreg Kroah-Hartman
115746a97191SGreg Kroah-Hartman for (i = 0; i < pagecount; i++) {
115846a97191SGreg Kroah-Hartman desc.range[i].len = pagebuffers[i].len;
115946a97191SGreg Kroah-Hartman desc.range[i].offset = pagebuffers[i].offset;
116046a97191SGreg Kroah-Hartman desc.range[i].pfn = pagebuffers[i].pfn;
116146a97191SGreg Kroah-Hartman }
116246a97191SGreg Kroah-Hartman
1163011a7c3cSK. Y. Srinivasan bufferlist[0].iov_base = &desc;
1164011a7c3cSK. Y. Srinivasan bufferlist[0].iov_len = descsize;
1165011a7c3cSK. Y. Srinivasan bufferlist[1].iov_base = buffer;
1166011a7c3cSK. Y. Srinivasan bufferlist[1].iov_len = bufferlen;
1167011a7c3cSK. Y. Srinivasan bufferlist[2].iov_base = &aligned_data;
1168011a7c3cSK. Y. Srinivasan bufferlist[2].iov_len = (packetlen_aligned - packetlen);
116946a97191SGreg Kroah-Hartman
1170b03afa57SAndrea Parri (Microsoft) return hv_ringbuffer_write(channel, bufferlist, 3, requestid, NULL);
117146a97191SGreg Kroah-Hartman }
117246a97191SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
117346a97191SGreg Kroah-Hartman
117446a97191SGreg Kroah-Hartman /*
117546a97191SGreg Kroah-Hartman * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
117646a97191SGreg Kroah-Hartman * using a GPADL Direct packet type.
1177d61031eeSK. Y. Srinivasan * The buffer includes the vmbus descriptor.
1178d61031eeSK. Y. Srinivasan */
vmbus_sendpacket_mpb_desc(struct vmbus_channel * channel,struct vmbus_packet_mpb_array * desc,u32 desc_size,void * buffer,u32 bufferlen,u64 requestid)1179d61031eeSK. Y. Srinivasan int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
1180d61031eeSK. Y. Srinivasan struct vmbus_packet_mpb_array *desc,
1181d61031eeSK. Y. Srinivasan u32 desc_size,
1182d61031eeSK. Y. Srinivasan void *buffer, u32 bufferlen, u64 requestid)
1183d61031eeSK. Y. Srinivasan {
1184d61031eeSK. Y. Srinivasan u32 packetlen;
1185d61031eeSK. Y. Srinivasan u32 packetlen_aligned;
1186d61031eeSK. Y. Srinivasan struct kvec bufferlist[3];
1187d61031eeSK. Y. Srinivasan u64 aligned_data = 0;
1188d61031eeSK. Y. Srinivasan
1189d61031eeSK. Y. Srinivasan packetlen = desc_size + bufferlen;
1190d61031eeSK. Y. Srinivasan packetlen_aligned = ALIGN(packetlen, sizeof(u64));
1191d61031eeSK. Y. Srinivasan
1192d61031eeSK. Y. Srinivasan /* Setup the descriptor */
1193d61031eeSK. Y. Srinivasan desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
1194d61031eeSK. Y. Srinivasan desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
1195bdc1dd47SStephen Hemminger desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
1196d61031eeSK. Y. Srinivasan desc->length8 = (u16)(packetlen_aligned >> 3);
1197e8b7db38SAndres Beltran desc->transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
119833d426a9SStephen Hemminger desc->reserved = 0;
1199d61031eeSK. Y. Srinivasan desc->rangecount = 1;
1200d61031eeSK. Y. Srinivasan
1201d61031eeSK. Y. Srinivasan bufferlist[0].iov_base = desc;
1202d61031eeSK. Y. Srinivasan bufferlist[0].iov_len = desc_size;
1203d61031eeSK. Y. Srinivasan bufferlist[1].iov_base = buffer;
1204d61031eeSK. Y. Srinivasan bufferlist[1].iov_len = bufferlen;
1205d61031eeSK. Y. Srinivasan bufferlist[2].iov_base = &aligned_data;
1206d61031eeSK. Y. Srinivasan bufferlist[2].iov_len = (packetlen_aligned - packetlen);
1207d61031eeSK. Y. Srinivasan
1208b03afa57SAndrea Parri (Microsoft) return hv_ringbuffer_write(channel, bufferlist, 3, requestid, NULL);
1209d61031eeSK. Y. Srinivasan }
1210d61031eeSK. Y. Srinivasan EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
1211d61031eeSK. Y. Srinivasan
121246a97191SGreg Kroah-Hartman /**
1213fe857bb4SDexuan Cui * __vmbus_recvpacket() - Retrieve the user packet on the specified channel
1214fe857bb4SDexuan Cui * @channel: Pointer to vmbus_channel structure
121546a97191SGreg Kroah-Hartman * @buffer: Pointer to the buffer you want to receive the data into.
1216fe857bb4SDexuan Cui * @bufferlen: Maximum size of what the buffer can hold.
1217fe857bb4SDexuan Cui * @buffer_actual_len: The actual size of the data after it was received.
121846a97191SGreg Kroah-Hartman * @requestid: Identifier of the request
1219fe857bb4SDexuan Cui * @raw: true means keep the vmpacket_descriptor header in the received data.
122046a97191SGreg Kroah-Hartman *
122146a97191SGreg Kroah-Hartman * Receives directly from the hyper-v vmbus and puts the data it received
122246a97191SGreg Kroah-Hartman * into Buffer. This will receive the data unparsed from hyper-v.
122346a97191SGreg Kroah-Hartman *
122446a97191SGreg Kroah-Hartman * Mainly used by Hyper-V drivers.
122546a97191SGreg Kroah-Hartman */
1226667d3740SVitaly Kuznetsov static inline int
__vmbus_recvpacket(struct vmbus_channel * channel,void * buffer,u32 bufferlen,u32 * buffer_actual_len,u64 * requestid,bool raw)1227667d3740SVitaly Kuznetsov __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
1228667d3740SVitaly Kuznetsov u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
1229667d3740SVitaly Kuznetsov bool raw)
123046a97191SGreg Kroah-Hartman {
12313372592aSK. Y. Srinivasan return hv_ringbuffer_read(channel, buffer, bufferlen,
12323372592aSK. Y. Srinivasan buffer_actual_len, requestid, raw);
123346a97191SGreg Kroah-Hartman
1234667d3740SVitaly Kuznetsov }
1235667d3740SVitaly Kuznetsov
vmbus_recvpacket(struct vmbus_channel * channel,void * buffer,u32 bufferlen,u32 * buffer_actual_len,u64 * requestid)1236667d3740SVitaly Kuznetsov int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
1237667d3740SVitaly Kuznetsov u32 bufferlen, u32 *buffer_actual_len,
1238667d3740SVitaly Kuznetsov u64 *requestid)
1239667d3740SVitaly Kuznetsov {
1240667d3740SVitaly Kuznetsov return __vmbus_recvpacket(channel, buffer, bufferlen,
1241667d3740SVitaly Kuznetsov buffer_actual_len, requestid, false);
124246a97191SGreg Kroah-Hartman }
124346a97191SGreg Kroah-Hartman EXPORT_SYMBOL(vmbus_recvpacket);
124446a97191SGreg Kroah-Hartman
124546a97191SGreg Kroah-Hartman /*
124646a97191SGreg Kroah-Hartman * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
124746a97191SGreg Kroah-Hartman */
vmbus_recvpacket_raw(struct vmbus_channel * channel,void * buffer,u32 bufferlen,u32 * buffer_actual_len,u64 * requestid)124846a97191SGreg Kroah-Hartman int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
124946a97191SGreg Kroah-Hartman u32 bufferlen, u32 *buffer_actual_len,
125046a97191SGreg Kroah-Hartman u64 *requestid)
125146a97191SGreg Kroah-Hartman {
1252667d3740SVitaly Kuznetsov return __vmbus_recvpacket(channel, buffer, bufferlen,
1253667d3740SVitaly Kuznetsov buffer_actual_len, requestid, true);
125446a97191SGreg Kroah-Hartman }
125546a97191SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
1256e8b7db38SAndres Beltran
1257e8b7db38SAndres Beltran /*
1258e8b7db38SAndres Beltran * vmbus_next_request_id - Returns a new request id. It is also
1259e8b7db38SAndres Beltran * the index at which the guest memory address is stored.
1260e8b7db38SAndres Beltran * Uses a spin lock to avoid race conditions.
1261bf5fd8caSAndrea Parri (Microsoft) * @channel: Pointer to the VMbus channel struct
1262e8b7db38SAndres Beltran * @rqst_add: Guest memory address to be stored in the array
1263e8b7db38SAndres Beltran */
vmbus_next_request_id(struct vmbus_channel * channel,u64 rqst_addr)1264bf5fd8caSAndrea Parri (Microsoft) u64 vmbus_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
1265e8b7db38SAndres Beltran {
1266bf5fd8caSAndrea Parri (Microsoft) struct vmbus_requestor *rqstor = &channel->requestor;
1267e8b7db38SAndres Beltran unsigned long flags;
1268e8b7db38SAndres Beltran u64 current_id;
1269e8b7db38SAndres Beltran
1270e8b7db38SAndres Beltran /* Check rqstor has been initialized */
1271e8b7db38SAndres Beltran if (!channel->rqstor_size)
1272e8b7db38SAndres Beltran return VMBUS_NO_RQSTOR;
1273e8b7db38SAndres Beltran
1274b91eaf72SAndrea Parri (Microsoft) lock_requestor(channel, flags);
1275e8b7db38SAndres Beltran current_id = rqstor->next_request_id;
1276e8b7db38SAndres Beltran
1277e8b7db38SAndres Beltran /* Requestor array is full */
1278e8b7db38SAndres Beltran if (current_id >= rqstor->size) {
1279b91eaf72SAndrea Parri (Microsoft) unlock_requestor(channel, flags);
1280e8b7db38SAndres Beltran return VMBUS_RQST_ERROR;
1281e8b7db38SAndres Beltran }
1282e8b7db38SAndres Beltran
1283e8b7db38SAndres Beltran rqstor->next_request_id = rqstor->req_arr[current_id];
1284e8b7db38SAndres Beltran rqstor->req_arr[current_id] = rqst_addr;
1285e8b7db38SAndres Beltran
1286e8b7db38SAndres Beltran /* The already held spin lock provides atomicity */
1287e8b7db38SAndres Beltran bitmap_set(rqstor->req_bitmap, current_id, 1);
1288e8b7db38SAndres Beltran
1289b91eaf72SAndrea Parri (Microsoft) unlock_requestor(channel, flags);
1290e8b7db38SAndres Beltran
1291e8b7db38SAndres Beltran /*
1292e8b7db38SAndres Beltran * Cannot return an ID of 0, which is reserved for an unsolicited
129382cd4bacSAndrea Parri (Microsoft) * message from Hyper-V; Hyper-V does not acknowledge (respond to)
129482cd4bacSAndrea Parri (Microsoft) * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED requests with ID of
129582cd4bacSAndrea Parri (Microsoft) * 0 sent by the guest.
1296e8b7db38SAndres Beltran */
1297e8b7db38SAndres Beltran return current_id + 1;
1298e8b7db38SAndres Beltran }
1299e8b7db38SAndres Beltran EXPORT_SYMBOL_GPL(vmbus_next_request_id);
1300e8b7db38SAndres Beltran
13010aadb6a7SAndrea Parri (Microsoft) /* As in vmbus_request_addr_match() but without the requestor lock */
__vmbus_request_addr_match(struct vmbus_channel * channel,u64 trans_id,u64 rqst_addr)13020aadb6a7SAndrea Parri (Microsoft) u64 __vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id,
13030aadb6a7SAndrea Parri (Microsoft) u64 rqst_addr)
1304e8b7db38SAndres Beltran {
1305bf5fd8caSAndrea Parri (Microsoft) struct vmbus_requestor *rqstor = &channel->requestor;
1306e8b7db38SAndres Beltran u64 req_addr;
1307e8b7db38SAndres Beltran
1308e8b7db38SAndres Beltran /* Check rqstor has been initialized */
1309e8b7db38SAndres Beltran if (!channel->rqstor_size)
1310e8b7db38SAndres Beltran return VMBUS_NO_RQSTOR;
1311e8b7db38SAndres Beltran
1312e8b7db38SAndres Beltran /* Hyper-V can send an unsolicited message with ID of 0 */
1313e8b7db38SAndres Beltran if (!trans_id)
131482cd4bacSAndrea Parri (Microsoft) return VMBUS_RQST_ERROR;
1315e8b7db38SAndres Beltran
1316e8b7db38SAndres Beltran /* Data corresponding to trans_id is stored at trans_id - 1 */
1317e8b7db38SAndres Beltran trans_id--;
1318e8b7db38SAndres Beltran
1319e8b7db38SAndres Beltran /* Invalid trans_id */
13200aadb6a7SAndrea Parri (Microsoft) if (trans_id >= rqstor->size || !test_bit(trans_id, rqstor->req_bitmap))
1321e8b7db38SAndres Beltran return VMBUS_RQST_ERROR;
1322e8b7db38SAndres Beltran
1323e8b7db38SAndres Beltran req_addr = rqstor->req_arr[trans_id];
13240aadb6a7SAndrea Parri (Microsoft) if (rqst_addr == VMBUS_RQST_ADDR_ANY || req_addr == rqst_addr) {
1325e8b7db38SAndres Beltran rqstor->req_arr[trans_id] = rqstor->next_request_id;
1326e8b7db38SAndres Beltran rqstor->next_request_id = trans_id;
1327e8b7db38SAndres Beltran
1328e8b7db38SAndres Beltran /* The already held spin lock provides atomicity */
1329e8b7db38SAndres Beltran bitmap_clear(rqstor->req_bitmap, trans_id, 1);
13300aadb6a7SAndrea Parri (Microsoft) }
1331e8b7db38SAndres Beltran
1332e8b7db38SAndres Beltran return req_addr;
1333e8b7db38SAndres Beltran }
13340aadb6a7SAndrea Parri (Microsoft) EXPORT_SYMBOL_GPL(__vmbus_request_addr_match);
13350aadb6a7SAndrea Parri (Microsoft)
13360aadb6a7SAndrea Parri (Microsoft) /*
13370aadb6a7SAndrea Parri (Microsoft) * vmbus_request_addr_match - Clears/removes @trans_id from the @channel's
13380aadb6a7SAndrea Parri (Microsoft) * requestor, provided the memory address stored at @trans_id equals @rqst_addr
13390aadb6a7SAndrea Parri (Microsoft) * (or provided @rqst_addr matches the sentinel value VMBUS_RQST_ADDR_ANY).
13400aadb6a7SAndrea Parri (Microsoft) *
13410aadb6a7SAndrea Parri (Microsoft) * Returns the memory address stored at @trans_id, or VMBUS_RQST_ERROR if
13420aadb6a7SAndrea Parri (Microsoft) * @trans_id is not contained in the requestor.
13430aadb6a7SAndrea Parri (Microsoft) *
13440aadb6a7SAndrea Parri (Microsoft) * Acquires and releases the requestor spin lock.
13450aadb6a7SAndrea Parri (Microsoft) */
vmbus_request_addr_match(struct vmbus_channel * channel,u64 trans_id,u64 rqst_addr)13460aadb6a7SAndrea Parri (Microsoft) u64 vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id,
13470aadb6a7SAndrea Parri (Microsoft) u64 rqst_addr)
13480aadb6a7SAndrea Parri (Microsoft) {
13490aadb6a7SAndrea Parri (Microsoft) unsigned long flags;
13500aadb6a7SAndrea Parri (Microsoft) u64 req_addr;
13510aadb6a7SAndrea Parri (Microsoft)
1352b91eaf72SAndrea Parri (Microsoft) lock_requestor(channel, flags);
13530aadb6a7SAndrea Parri (Microsoft) req_addr = __vmbus_request_addr_match(channel, trans_id, rqst_addr);
1354b91eaf72SAndrea Parri (Microsoft) unlock_requestor(channel, flags);
13550aadb6a7SAndrea Parri (Microsoft)
13560aadb6a7SAndrea Parri (Microsoft) return req_addr;
13570aadb6a7SAndrea Parri (Microsoft) }
13580aadb6a7SAndrea Parri (Microsoft) EXPORT_SYMBOL_GPL(vmbus_request_addr_match);
13590aadb6a7SAndrea Parri (Microsoft)
13600aadb6a7SAndrea Parri (Microsoft) /*
13610aadb6a7SAndrea Parri (Microsoft) * vmbus_request_addr - Returns the memory address stored at @trans_id
13620aadb6a7SAndrea Parri (Microsoft) * in @rqstor. Uses a spin lock to avoid race conditions.
13630aadb6a7SAndrea Parri (Microsoft) * @channel: Pointer to the VMbus channel struct
13640aadb6a7SAndrea Parri (Microsoft) * @trans_id: Request id sent back from Hyper-V. Becomes the requestor's
13650aadb6a7SAndrea Parri (Microsoft) * next request id.
13660aadb6a7SAndrea Parri (Microsoft) */
vmbus_request_addr(struct vmbus_channel * channel,u64 trans_id)13670aadb6a7SAndrea Parri (Microsoft) u64 vmbus_request_addr(struct vmbus_channel *channel, u64 trans_id)
13680aadb6a7SAndrea Parri (Microsoft) {
13690aadb6a7SAndrea Parri (Microsoft) return vmbus_request_addr_match(channel, trans_id, VMBUS_RQST_ADDR_ANY);
13700aadb6a7SAndrea Parri (Microsoft) }
1371e8b7db38SAndres Beltran EXPORT_SYMBOL_GPL(vmbus_request_addr);
1372