blob: dc5c35210c16a2f0500145ac0dd8771b514fa783 [file] [log] [blame]
Thomas Gleixner3b20eb22019-05-29 16:57:35 -07001// SPDX-License-Identifier: GPL-2.0-only
Hank Janssen3e7ee492009-07-13 16:02:34 -07002/*
Hank Janssen3e7ee492009-07-13 16:02:34 -07003 * Copyright (c) 2009, Microsoft Corporation.
4 *
Hank Janssen3e7ee492009-07-13 16:02:34 -07005 * Authors:
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssen3e7ee492009-07-13 16:02:34 -07008 */
Hank Janssen0a466182011-03-29 13:58:47 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Greg Kroah-Hartman5654e932009-07-14 15:08:20 -070011#include <linux/kernel.h>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080012#include <linux/sched.h>
13#include <linux/wait.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070014#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Hank Janssenc88c4e42010-05-04 15:55:05 -070016#include <linux/module.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070017#include <linux/hyperv.h>
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -080018#include <linux/uio.h>
Dexuan Cui63d55b22015-12-14 16:01:47 -080019#include <linux/interrupt.h>
Tianyu Land4dccf32021-10-25 08:21:09 -040020#include <linux/set_memory.h>
Michael Kelley6ba34172018-08-02 03:08:24 +000021#include <asm/page.h>
Andrea Parri (Microsoft)5bf74682020-06-17 18:46:35 +020022#include <asm/mshyperv.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070023
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070024#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070025
Hank Janssen3e189512010-03-04 22:11:00 +000026/*
Boqun Fengc1135c72020-09-16 11:48:09 +080027 * hv_gpadl_size - Return the real size of a gpadl, the size that Hyper-V uses
28 *
29 * For BUFFER gpadl, Hyper-V uses the exact same size as the guest does.
30 *
31 * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the header
32 * (because of the alignment requirement), however, the hypervisor only
33 * uses the first HV_HYP_PAGE_SIZE as the header, therefore leaving a
34 * (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap. And since there are two rings in a
35 * ringbuffer, the total size for a RING gpadl that Hyper-V uses is the
36 * total size that the guest uses minus twice of the gap size.
37 */
38static inline u32 hv_gpadl_size(enum hv_gpadl_type type, u32 size)
39{
40 switch (type) {
41 case HV_GPADL_BUFFER:
42 return size;
43 case HV_GPADL_RING:
44 /* The size of a ringbuffer must be page-aligned */
45 BUG_ON(size % PAGE_SIZE);
46 /*
47 * Two things to notice here:
48 * 1) We're processing two ring buffers as a unit
49 * 2) We're skipping any space larger than HV_HYP_PAGE_SIZE in
50 * the first guest-size page of each of the two ring buffers.
51 * So we effectively subtract out two guest-size pages, and add
52 * back two Hyper-V size pages.
53 */
54 return size - 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE);
55 }
56 BUG();
57 return 0;
58}
59
60/*
61 * hv_ring_gpadl_send_hvpgoffset - Calculate the send offset (in unit of
62 * HV_HYP_PAGE) in a ring gpadl based on the
63 * offset in the guest
64 *
65 * @offset: the offset (in bytes) where the send ringbuffer starts in the
66 * virtual address space of the guest
67 */
68static inline u32 hv_ring_gpadl_send_hvpgoffset(u32 offset)
69{
70
71 /*
72 * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the
73 * header (because of the alignment requirement), however, the
74 * hypervisor only uses the first HV_HYP_PAGE_SIZE as the header,
75 * therefore leaving a (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap.
76 *
77 * And to calculate the effective send offset in gpadl, we need to
78 * substract this gap.
79 */
80 return (offset - (PAGE_SIZE - HV_HYP_PAGE_SIZE)) >> HV_HYP_PAGE_SHIFT;
81}
82
83/*
84 * hv_gpadl_hvpfn - Return the Hyper-V page PFN of the @i th Hyper-V page in
85 * the gpadl
86 *
87 * @type: the type of the gpadl
88 * @kbuffer: the pointer to the gpadl in the guest
89 * @size: the total size (in bytes) of the gpadl
90 * @send_offset: the offset (in bytes) where the send ringbuffer starts in the
91 * virtual address space of the guest
92 * @i: the index
93 */
94static inline u64 hv_gpadl_hvpfn(enum hv_gpadl_type type, void *kbuffer,
95 u32 size, u32 send_offset, int i)
96{
97 int send_idx = hv_ring_gpadl_send_hvpgoffset(send_offset);
98 unsigned long delta = 0UL;
99
100 switch (type) {
101 case HV_GPADL_BUFFER:
102 break;
103 case HV_GPADL_RING:
104 if (i == 0)
105 delta = 0;
106 else if (i <= send_idx)
107 delta = PAGE_SIZE - HV_HYP_PAGE_SIZE;
108 else
109 delta = 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE);
110 break;
111 default:
112 BUG();
113 break;
114 }
115
116 return virt_to_hvpfn(kbuffer + delta + (HV_HYP_PAGE_SIZE * i));
117}
118
119/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700120 * vmbus_setevent- Trigger an event notification on the specified
Hank Janssen3e189512010-03-04 22:11:00 +0000121 * channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700122 */
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -0800123void vmbus_setevent(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700124{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700125 struct hv_monitor_page *monitorpage;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700126
Vitaly Kuznetsov991f8f12017-10-29 12:21:16 -0700127 trace_vmbus_setevent(channel);
128
K. Y. Srinivasan37242872016-07-01 16:26:37 -0700129 /*
130 * For channels marked as in "low latency" mode
131 * bypass the monitor page mechanism.
132 */
Stephen Hemminger5c1bec62017-02-05 17:20:31 -0700133 if (channel->offermsg.monitor_allocated && !channel->low_latency) {
134 vmbus_send_interrupt(channel->offermsg.child_relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700135
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -0700136 /* Get the child to parent monitor page */
137 monitorpage = vmbus_connection.monitor_pages[1];
Hank Janssen3e7ee492009-07-13 16:02:34 -0700138
Olaf Hering22356582011-03-21 14:41:37 +0100139 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800140 (unsigned long *)&monitorpage->trigger_group
141 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -0400142
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700143 } else {
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -0800144 vmbus_set_event(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700145 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700146}
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -0800147EXPORT_SYMBOL_GPL(vmbus_setevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700148
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700149/* vmbus_free_ring - drop mapping of ring buffer */
150void vmbus_free_ring(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700151{
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700152 hv_ringbuffer_cleanup(&channel->outbound);
153 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700154
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700155 if (channel->ringbuffer_page) {
156 __free_pages(channel->ringbuffer_page,
157 get_order(channel->ringbuffer_pagecount
158 << PAGE_SHIFT));
159 channel->ringbuffer_page = NULL;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700160 }
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700161}
162EXPORT_SYMBOL_GPL(vmbus_free_ring);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700163
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700164/* vmbus_alloc_ring - allocate and map pages for ring buffer */
165int vmbus_alloc_ring(struct vmbus_channel *newchannel,
166 u32 send_size, u32 recv_size)
167{
168 struct page *page;
169 int order;
170
171 if (send_size % PAGE_SIZE || recv_size % PAGE_SIZE)
172 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700173
Bill Pemberton454f18a2009-07-27 16:47:24 -0400174 /* Allocate the ring buffer */
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700175 order = get_order(send_size + recv_size);
K. Y. Srinivasan294409d2015-05-31 21:27:03 -0700176 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
Stephen Hemminger52a42c22018-09-14 09:10:16 -0700177 GFP_KERNEL|__GFP_ZERO, order);
K. Y. Srinivasan294409d2015-05-31 21:27:03 -0700178
179 if (!page)
Stephen Hemminger52a42c22018-09-14 09:10:16 -0700180 page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order);
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800181
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700182 if (!page)
183 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700184
Stephen Hemminger52a42c22018-09-14 09:10:16 -0700185 newchannel->ringbuffer_page = page;
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700186 newchannel->ringbuffer_pagecount = (send_size + recv_size) >> PAGE_SHIFT;
187 newchannel->ringbuffer_send_offset = send_size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700188
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700189 return 0;
190}
191EXPORT_SYMBOL_GPL(vmbus_alloc_ring);
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700192
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800193/* Used for Hyper-V Socket: a guest client's connect() to the host */
Andy Shevchenko593db802019-01-10 16:25:32 +0200194int vmbus_send_tl_connect_request(const guid_t *shv_guest_servie_id,
195 const guid_t *shv_host_servie_id)
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800196{
197 struct vmbus_channel_tl_connect_request conn_msg;
Vitaly Kuznetsov98f31a02017-10-29 12:21:15 -0700198 int ret;
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800199
200 memset(&conn_msg, 0, sizeof(conn_msg));
201 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
202 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
203 conn_msg.host_service_id = *shv_host_servie_id;
204
Vitaly Kuznetsov98f31a02017-10-29 12:21:15 -0700205 ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
206
207 trace_vmbus_send_tl_connect_request(&conn_msg, ret);
208
209 return ret;
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800210}
211EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
212
Andrea Parri (Microsoft)870ced02021-04-16 16:34:48 +0200213static int send_modifychannel_without_ack(struct vmbus_channel *channel, u32 target_vp)
214{
215 struct vmbus_channel_modifychannel msg;
216 int ret;
217
218 memset(&msg, 0, sizeof(msg));
219 msg.header.msgtype = CHANNELMSG_MODIFYCHANNEL;
220 msg.child_relid = channel->offermsg.child_relid;
221 msg.target_vp = target_vp;
222
223 ret = vmbus_post_msg(&msg, sizeof(msg), true);
224 trace_vmbus_send_modifychannel(&msg, ret);
225
226 return ret;
227}
228
229static int send_modifychannel_with_ack(struct vmbus_channel *channel, u32 target_vp)
230{
231 struct vmbus_channel_modifychannel *msg;
232 struct vmbus_channel_msginfo *info;
233 unsigned long flags;
234 int ret;
235
236 info = kzalloc(sizeof(struct vmbus_channel_msginfo) +
237 sizeof(struct vmbus_channel_modifychannel),
238 GFP_KERNEL);
239 if (!info)
240 return -ENOMEM;
241
242 init_completion(&info->waitevent);
243 info->waiting_channel = channel;
244
245 msg = (struct vmbus_channel_modifychannel *)info->msg;
246 msg->header.msgtype = CHANNELMSG_MODIFYCHANNEL;
247 msg->child_relid = channel->offermsg.child_relid;
248 msg->target_vp = target_vp;
249
250 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
251 list_add_tail(&info->msglistentry, &vmbus_connection.chn_msg_list);
252 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
253
254 ret = vmbus_post_msg(msg, sizeof(*msg), true);
255 trace_vmbus_send_modifychannel(msg, ret);
256 if (ret != 0) {
257 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
258 list_del(&info->msglistentry);
259 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
260 goto free_info;
261 }
262
263 /*
264 * Release channel_mutex; otherwise, vmbus_onoffer_rescind() could block on
265 * the mutex and be unable to signal the completion.
266 *
267 * See the caller target_cpu_store() for information about the usage of the
268 * mutex.
269 */
270 mutex_unlock(&vmbus_connection.channel_mutex);
271 wait_for_completion(&info->waitevent);
272 mutex_lock(&vmbus_connection.channel_mutex);
273
274 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
275 list_del(&info->msglistentry);
276 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
277
278 if (info->response.modify_response.status)
279 ret = -EAGAIN;
280
281free_info:
282 kfree(info);
283 return ret;
284}
285
Hank Janssen3e189512010-03-04 22:11:00 +0000286/*
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +0200287 * Set/change the vCPU (@target_vp) the channel (@child_relid) will interrupt.
288 *
Andrea Parri (Microsoft)870ced02021-04-16 16:34:48 +0200289 * CHANNELMSG_MODIFYCHANNEL messages are aynchronous. When VMbus version 5.3
290 * or later is negotiated, Hyper-V always sends an ACK in response to such a
291 * message. For VMbus version 5.2 and earlier, it never sends an ACK. With-
292 * out an ACK, we can not know when the host will stop interrupting the "old"
293 * vCPU and start interrupting the "new" vCPU for the given channel.
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +0200294 *
295 * The CHANNELMSG_MODIFYCHANNEL message type is supported since VMBus version
296 * VERSION_WIN10_V4_1.
297 */
Andrea Parri (Microsoft)870ced02021-04-16 16:34:48 +0200298int vmbus_send_modifychannel(struct vmbus_channel *channel, u32 target_vp)
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +0200299{
Andrea Parri (Microsoft)870ced02021-04-16 16:34:48 +0200300 if (vmbus_proto_version >= VERSION_WIN10_V5_3)
301 return send_modifychannel_with_ack(channel, target_vp);
302 return send_modifychannel_without_ack(channel, target_vp);
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +0200303}
304EXPORT_SYMBOL_GPL(vmbus_send_modifychannel);
305
306/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700307 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700308 */
Boqun Fengc1135c72020-09-16 11:48:09 +0800309static int create_gpadl_header(enum hv_gpadl_type type, void *kbuffer,
310 u32 size, u32 send_offset,
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700311 struct vmbus_channel_msginfo **msginfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700312{
313 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700314 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700315 struct vmbus_channel_gpadl_header *gpadl_header;
316 struct vmbus_channel_gpadl_body *gpadl_body;
317 struct vmbus_channel_msginfo *msgheader;
318 struct vmbus_channel_msginfo *msgbody = NULL;
319 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700320
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700321 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700322
Boqun Fengc1135c72020-09-16 11:48:09 +0800323 pagecount = hv_gpadl_size(type, size) >> HV_HYP_PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700324
Bill Pemberton454f18a2009-07-27 16:47:24 -0400325 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700326 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700327 sizeof(struct vmbus_channel_gpadl_header) -
328 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700329 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700330
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700331 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700332 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400333 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700334 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700335 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700336 sizeof(struct gpa_range) + pfncount * sizeof(u64);
337 msgheader = kzalloc(msgsize, GFP_KERNEL);
338 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400339 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700340
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800341 INIT_LIST_HEAD(&msgheader->submsglist);
342 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700343
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700344 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800345 msgheader->msg;
346 gpadl_header->rangecount = 1;
347 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700348 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800349 gpadl_header->range[0].byte_offset = 0;
Boqun Fengc1135c72020-09-16 11:48:09 +0800350 gpadl_header->range[0].byte_count = hv_gpadl_size(type, size);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700351 for (i = 0; i < pfncount; i++)
Boqun Fengc1135c72020-09-16 11:48:09 +0800352 gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn(
353 type, kbuffer, size, send_offset, i);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700354 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700355
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700356 pfnsum = pfncount;
357 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700358
Bill Pemberton454f18a2009-07-27 16:47:24 -0400359 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700360 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700361 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700362 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700363
Bill Pemberton454f18a2009-07-27 16:47:24 -0400364 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700365 while (pfnleft) {
366 if (pfnleft > pfncount)
367 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700368 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700369 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700370
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700371 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700372 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700373 pfncurr * sizeof(u64);
374 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700375
376 if (!msgbody) {
377 struct vmbus_channel_msginfo *pos = NULL;
378 struct vmbus_channel_msginfo *tmp = NULL;
379 /*
380 * Free up all the allocated messages.
381 */
382 list_for_each_entry_safe(pos, tmp,
383 &msgheader->submsglist,
384 msglistentry) {
385
386 list_del(&pos->msglistentry);
387 kfree(pos);
388 }
389
Bill Pembertond1c250b2010-05-05 15:27:35 -0400390 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700391 }
392
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800393 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700394 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800395 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700396
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700397 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700398 * Gpadl is u32 and we are using a pointer which could
399 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700400 * This is governed by the guest/host protocol and
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700401 * so the hypervisor guarantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700402 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700403 for (i = 0; i < pfncurr; i++)
Boqun Fengc1135c72020-09-16 11:48:09 +0800404 gpadl_body->pfn[i] = hv_gpadl_hvpfn(type,
405 kbuffer, size, send_offset, pfnsum + i);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700406
Bill Pemberton454f18a2009-07-27 16:47:24 -0400407 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800408 list_add_tail(&msgbody->msglistentry,
409 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700410 pfnsum += pfncurr;
411 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700412 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700413 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400414 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700415 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700416 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700417 sizeof(struct gpa_range) + pagecount * sizeof(u64);
418 msgheader = kzalloc(msgsize, GFP_KERNEL);
419 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400420 goto nomem;
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700421
422 INIT_LIST_HEAD(&msgheader->submsglist);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800423 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700424
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700425 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800426 msgheader->msg;
427 gpadl_header->rangecount = 1;
428 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700429 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800430 gpadl_header->range[0].byte_offset = 0;
Boqun Fengc1135c72020-09-16 11:48:09 +0800431 gpadl_header->range[0].byte_count = hv_gpadl_size(type, size);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700432 for (i = 0; i < pagecount; i++)
Boqun Fengc1135c72020-09-16 11:48:09 +0800433 gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn(
434 type, kbuffer, size, send_offset, i);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700435
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700436 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700437 }
438
439 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400440nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700441 kfree(msgheader);
442 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400443 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700444}
445
Hank Janssen3e189512010-03-04 22:11:00 +0000446/*
Boqun Fengc1135c72020-09-16 11:48:09 +0800447 * __vmbus_establish_gpadl - Establish a GPADL for a buffer or ringbuffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700448 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700449 * @channel: a channel
Boqun Fengc1135c72020-09-16 11:48:09 +0800450 * @type: the type of the corresponding GPADL, only meaningful for the guest.
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800451 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700452 * @size: page-size multiple
Boqun Fengc1135c72020-09-16 11:48:09 +0800453 * @send_offset: the offset (in bytes) where the send ring buffer starts,
Vasanthf850a4c2021-02-19 22:43:11 +0530454 * should be 0 for BUFFER type gpadl
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700455 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700456 */
Boqun Fengc1135c72020-09-16 11:48:09 +0800457static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
458 enum hv_gpadl_type type, void *kbuffer,
459 u32 size, u32 send_offset,
Tianyu Land4dccf32021-10-25 08:21:09 -0400460 struct vmbus_gpadl *gpadl)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700461{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700462 struct vmbus_channel_gpadl_header *gpadlmsg;
463 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700464 struct vmbus_channel_msginfo *msginfo = NULL;
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700465 struct vmbus_channel_msginfo *submsginfo, *tmp;
Bill Pemberton53af5452009-09-11 21:46:44 -0400466 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700467 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700468 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400469 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700470
K. Y. Srinivasan9f52a162015-01-09 23:54:33 -0800471 next_gpadl_handle =
472 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700473
Boqun Fengc1135c72020-09-16 11:48:09 +0800474 ret = create_gpadl_header(type, kbuffer, size, send_offset, &msginfo);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400475 if (ret)
476 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700477
Tianyu Land4dccf32021-10-25 08:21:09 -0400478 ret = set_memory_decrypted((unsigned long)kbuffer,
479 PFN_UP(size));
480 if (ret) {
481 dev_warn(&channel->device_obj->device,
482 "Failed to set host visibility for new GPADL %d.\n",
483 ret);
484 return ret;
485 }
486
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700487 init_completion(&msginfo->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800488 msginfo->waiting_channel = channel;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400489
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800490 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
491 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
492 gpadlmsg->child_relid = channel->offermsg.child_relid;
493 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700494
Hank Janssen3e7ee492009-07-13 16:02:34 -0700495
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800496 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800497 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800498 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700499
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800500 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700501
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -0700502 if (channel->rescind) {
503 ret = -ENODEV;
504 goto cleanup;
505 }
506
Haiyang Zhangc6977672011-01-26 12:12:08 -0800507 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800508 sizeof(*msginfo), true);
Vitaly Kuznetsov69edbd52017-10-29 12:21:11 -0700509
510 trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
511
Hank Janssen98e08702011-03-29 13:58:44 -0700512 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700513 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700514
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700515 list_for_each(curr, &msginfo->submsglist) {
516 submsginfo = (struct vmbus_channel_msginfo *)curr;
517 gpadl_body =
518 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Bill Pemberton53af5452009-09-11 21:46:44 -0400519
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700520 gpadl_body->header.msgtype =
521 CHANNELMSG_GPADL_BODY;
522 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700523
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700524 ret = vmbus_post_msg(gpadl_body,
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800525 submsginfo->msgsize - sizeof(*submsginfo),
526 true);
Vitaly Kuznetsov69edbd52017-10-29 12:21:11 -0700527
528 trace_vmbus_establish_gpadl_body(gpadl_body, ret);
529
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700530 if (ret != 0)
531 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700532
Hank Janssen3e7ee492009-07-13 16:02:34 -0700533 }
K. Y. Srinivasan72c6b712014-08-27 16:25:34 -0700534 wait_for_completion(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700535
Dexuan Cuieceb0592018-11-26 02:29:56 +0000536 if (msginfo->response.gpadl_created.creation_status != 0) {
537 pr_err("Failed to establish GPADL: err = 0x%x\n",
538 msginfo->response.gpadl_created.creation_status);
539
540 ret = -EDQUOT;
541 goto cleanup;
542 }
543
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800544 if (channel->rescind) {
545 ret = -ENODEV;
546 goto cleanup;
547 }
548
Bill Pemberton454f18a2009-07-27 16:47:24 -0400549 /* At this point, we received the gpadl created msg */
Tianyu Land4dccf32021-10-25 08:21:09 -0400550 gpadl->gpadl_handle = gpadlmsg->gpadl;
551 gpadl->buffer = kbuffer;
552 gpadl->size = size;
553
Hank Janssen3e7ee492009-07-13 16:02:34 -0700554
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700555cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800556 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800557 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800558 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700559 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
560 msglistentry) {
561 kfree(submsginfo);
562 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700563
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700564 kfree(msginfo);
Tianyu Land4dccf32021-10-25 08:21:09 -0400565
566 if (ret)
567 set_memory_encrypted((unsigned long)kbuffer,
568 PFN_UP(size));
569
Hank Janssen3e7ee492009-07-13 16:02:34 -0700570 return ret;
571}
Boqun Fengc1135c72020-09-16 11:48:09 +0800572
573/*
574 * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
575 *
576 * @channel: a channel
577 * @kbuffer: from kmalloc or vmalloc
578 * @size: page-size multiple
579 * @gpadl_handle: some funky thing
580 */
581int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Tianyu Land4dccf32021-10-25 08:21:09 -0400582 u32 size, struct vmbus_gpadl *gpadl)
Boqun Fengc1135c72020-09-16 11:48:09 +0800583{
584 return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER, kbuffer, size,
Tianyu Land4dccf32021-10-25 08:21:09 -0400585 0U, gpadl);
Boqun Fengc1135c72020-09-16 11:48:09 +0800586}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700587EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700588
Andres Beltrane8b7db32020-11-09 11:04:00 +0100589/**
590 * request_arr_init - Allocates memory for the requestor array. Each slot
591 * keeps track of the next available slot in the array. Initially, each
592 * slot points to the next one (as in a Linked List). The last slot
593 * does not point to anything, so its value is U64_MAX by default.
594 * @size The size of the array
595 */
596static u64 *request_arr_init(u32 size)
597{
598 int i;
599 u64 *req_arr;
600
601 req_arr = kcalloc(size, sizeof(u64), GFP_KERNEL);
602 if (!req_arr)
603 return NULL;
604
605 for (i = 0; i < size - 1; i++)
606 req_arr[i] = i + 1;
607
608 /* Last slot (no more available slots) */
609 req_arr[i] = U64_MAX;
610
611 return req_arr;
612}
613
614/*
615 * vmbus_alloc_requestor - Initializes @rqstor's fields.
616 * Index 0 is the first free slot
617 * @size: Size of the requestor array
618 */
619static int vmbus_alloc_requestor(struct vmbus_requestor *rqstor, u32 size)
620{
621 u64 *rqst_arr;
622 unsigned long *bitmap;
623
624 rqst_arr = request_arr_init(size);
625 if (!rqst_arr)
626 return -ENOMEM;
627
628 bitmap = bitmap_zalloc(size, GFP_KERNEL);
629 if (!bitmap) {
630 kfree(rqst_arr);
631 return -ENOMEM;
632 }
633
634 rqstor->req_arr = rqst_arr;
635 rqstor->req_bitmap = bitmap;
636 rqstor->size = size;
637 rqstor->next_request_id = 0;
638 spin_lock_init(&rqstor->req_lock);
639
640 return 0;
641}
642
643/*
644 * vmbus_free_requestor - Frees memory allocated for @rqstor
645 * @rqstor: Pointer to the requestor struct
646 */
647static void vmbus_free_requestor(struct vmbus_requestor *rqstor)
648{
649 kfree(rqstor->req_arr);
650 bitmap_free(rqstor->req_bitmap);
651}
652
Boqun Fengedd9bbc2020-09-16 11:48:08 +0800653static int __vmbus_open(struct vmbus_channel *newchannel,
654 void *userdata, u32 userdatalen,
655 void (*onchannelcallback)(void *context), void *context)
656{
657 struct vmbus_channel_open_channel *open_msg;
658 struct vmbus_channel_msginfo *open_info = NULL;
659 struct page *page = newchannel->ringbuffer_page;
660 u32 send_pages, recv_pages;
661 unsigned long flags;
662 int err;
663
664 if (userdatalen > MAX_USER_DEFINED_BYTES)
665 return -EINVAL;
666
667 send_pages = newchannel->ringbuffer_send_offset;
668 recv_pages = newchannel->ringbuffer_pagecount - send_pages;
669
670 if (newchannel->state != CHANNEL_OPEN_STATE)
671 return -EINVAL;
672
Andres Beltrane8b7db32020-11-09 11:04:00 +0100673 /* Create and init requestor */
674 if (newchannel->rqstor_size) {
675 if (vmbus_alloc_requestor(&newchannel->requestor, newchannel->rqstor_size))
676 return -ENOMEM;
677 }
678
Boqun Fengedd9bbc2020-09-16 11:48:08 +0800679 newchannel->state = CHANNEL_OPENING_STATE;
680 newchannel->onchannel_callback = onchannelcallback;
681 newchannel->channel_callback_context = context;
682
Andres Beltranadae1e92021-04-08 18:14:39 +0200683 if (!newchannel->max_pkt_size)
684 newchannel->max_pkt_size = VMBUS_DEFAULT_MAX_PKT_SIZE;
685
Boqun Fengedd9bbc2020-09-16 11:48:08 +0800686 /* Establish the gpadl for the ring buffer */
Tianyu Land4dccf32021-10-25 08:21:09 -0400687 newchannel->ringbuffer_gpadlhandle.gpadl_handle = 0;
Boqun Fengedd9bbc2020-09-16 11:48:08 +0800688
Boqun Fengc1135c72020-09-16 11:48:09 +0800689 err = __vmbus_establish_gpadl(newchannel, HV_GPADL_RING,
690 page_address(newchannel->ringbuffer_page),
691 (send_pages + recv_pages) << PAGE_SHIFT,
692 newchannel->ringbuffer_send_offset << PAGE_SHIFT,
693 &newchannel->ringbuffer_gpadlhandle);
Boqun Fengedd9bbc2020-09-16 11:48:08 +0800694 if (err)
695 goto error_clean_ring;
696
Tianyu Lan9a879772021-10-25 08:21:14 -0400697 err = hv_ringbuffer_init(&newchannel->outbound,
698 page, send_pages, 0);
699 if (err)
700 goto error_free_gpadl;
701
702 err = hv_ringbuffer_init(&newchannel->inbound, &page[send_pages],
703 recv_pages, newchannel->max_pkt_size);
704 if (err)
705 goto error_free_gpadl;
706
Boqun Fengedd9bbc2020-09-16 11:48:08 +0800707 /* Create and init the channel open message */
Andrea Parri (Microsoft)e99c4af2020-12-09 08:08:22 +0100708 open_info = kzalloc(sizeof(*open_info) +
Boqun Fengedd9bbc2020-09-16 11:48:08 +0800709 sizeof(struct vmbus_channel_open_channel),
710 GFP_KERNEL);
711 if (!open_info) {
712 err = -ENOMEM;
713 goto error_free_gpadl;
714 }
715
716 init_completion(&open_info->waitevent);
717 open_info->waiting_channel = newchannel;
718
719 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
720 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
721 open_msg->openid = newchannel->offermsg.child_relid;
722 open_msg->child_relid = newchannel->offermsg.child_relid;
Tianyu Land4dccf32021-10-25 08:21:09 -0400723 open_msg->ringbuffer_gpadlhandle
724 = newchannel->ringbuffer_gpadlhandle.gpadl_handle;
Boqun Fengc1135c72020-09-16 11:48:09 +0800725 /*
726 * The unit of ->downstream_ringbuffer_pageoffset is HV_HYP_PAGE and
727 * the unit of ->ringbuffer_send_offset (i.e. send_pages) is PAGE, so
728 * here we calculate it into HV_HYP_PAGE.
729 */
730 open_msg->downstream_ringbuffer_pageoffset =
731 hv_ring_gpadl_send_hvpgoffset(send_pages << PAGE_SHIFT);
Boqun Fengedd9bbc2020-09-16 11:48:08 +0800732 open_msg->target_vp = hv_cpu_number_to_vp_number(newchannel->target_cpu);
733
734 if (userdatalen)
735 memcpy(open_msg->userdata, userdata, userdatalen);
736
737 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
738 list_add_tail(&open_info->msglistentry,
739 &vmbus_connection.chn_msg_list);
740 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
741
742 if (newchannel->rescind) {
743 err = -ENODEV;
Dan Carpenter3e9bf432021-04-13 13:50:04 +0300744 goto error_clean_msglist;
Boqun Fengedd9bbc2020-09-16 11:48:08 +0800745 }
746
747 err = vmbus_post_msg(open_msg,
748 sizeof(struct vmbus_channel_open_channel), true);
749
750 trace_vmbus_open(open_msg, err);
751
752 if (err != 0)
753 goto error_clean_msglist;
754
755 wait_for_completion(&open_info->waitevent);
756
757 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
758 list_del(&open_info->msglistentry);
759 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
760
761 if (newchannel->rescind) {
762 err = -ENODEV;
763 goto error_free_info;
764 }
765
766 if (open_info->response.open_result.status) {
767 err = -EAGAIN;
768 goto error_free_info;
769 }
770
771 newchannel->state = CHANNEL_OPENED_STATE;
772 kfree(open_info);
773 return 0;
774
775error_clean_msglist:
776 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
777 list_del(&open_info->msglistentry);
778 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
779error_free_info:
780 kfree(open_info);
781error_free_gpadl:
Tianyu Land4dccf32021-10-25 08:21:09 -0400782 vmbus_teardown_gpadl(newchannel, &newchannel->ringbuffer_gpadlhandle);
Boqun Fengedd9bbc2020-09-16 11:48:08 +0800783error_clean_ring:
784 hv_ringbuffer_cleanup(&newchannel->outbound);
785 hv_ringbuffer_cleanup(&newchannel->inbound);
Andres Beltrane8b7db32020-11-09 11:04:00 +0100786 vmbus_free_requestor(&newchannel->requestor);
Boqun Fengedd9bbc2020-09-16 11:48:08 +0800787 newchannel->state = CHANNEL_OPEN_STATE;
788 return err;
789}
790
791/*
792 * vmbus_connect_ring - Open the channel but reuse ring buffer
793 */
794int vmbus_connect_ring(struct vmbus_channel *newchannel,
795 void (*onchannelcallback)(void *context), void *context)
796{
797 return __vmbus_open(newchannel, NULL, 0, onchannelcallback, context);
798}
799EXPORT_SYMBOL_GPL(vmbus_connect_ring);
800
801/*
802 * vmbus_open - Open the specified channel.
803 */
804int vmbus_open(struct vmbus_channel *newchannel,
805 u32 send_ringbuffer_size, u32 recv_ringbuffer_size,
806 void *userdata, u32 userdatalen,
807 void (*onchannelcallback)(void *context), void *context)
808{
809 int err;
810
811 err = vmbus_alloc_ring(newchannel, send_ringbuffer_size,
812 recv_ringbuffer_size);
813 if (err)
814 return err;
815
816 err = __vmbus_open(newchannel, userdata, userdatalen,
817 onchannelcallback, context);
818 if (err)
819 vmbus_free_ring(newchannel);
820
821 return err;
822}
823EXPORT_SYMBOL_GPL(vmbus_open);
824
Hank Janssen3e189512010-03-04 22:11:00 +0000825/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700826 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700827 */
Tianyu Land4dccf32021-10-25 08:21:09 -0400828int vmbus_teardown_gpadl(struct vmbus_channel *channel, struct vmbus_gpadl *gpadl)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700829{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700830 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700831 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700832 unsigned long flags;
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700833 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700834
Andrea Parri (Microsoft)e99c4af2020-12-09 08:08:22 +0100835 info = kzalloc(sizeof(*info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700836 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400837 if (!info)
838 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700839
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700840 init_completion(&info->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800841 info->waiting_channel = channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700842
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800843 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700844
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800845 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
846 msg->child_relid = channel->offermsg.child_relid;
Tianyu Land4dccf32021-10-25 08:21:09 -0400847 msg->gpadl = gpadl->gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700848
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800849 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800850 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800851 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800852 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -0700853
854 if (channel->rescind)
855 goto post_msg_err;
856
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800857 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
858 true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700859
Vitaly Kuznetsov09cdf8f2017-10-29 12:21:12 -0700860 trace_vmbus_teardown_gpadl(msg, ret);
861
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700862 if (ret)
863 goto post_msg_err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700864
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700865 wait_for_completion(&info->waitevent);
866
Tianyu Land4dccf32021-10-25 08:21:09 -0400867 gpadl->gpadl_handle = 0;
868
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700869post_msg_err:
K. Y. Srinivasan5e030d52017-03-12 20:00:30 -0700870 /*
871 * If the channel has been rescinded;
872 * we will be awakened by the rescind
873 * handler; set the error code to zero so we don't leak memory.
874 */
875 if (channel->rescind)
876 ret = 0;
877
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800878 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800879 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800880 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700881
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700882 kfree(info);
Tianyu Land4dccf32021-10-25 08:21:09 -0400883
884 ret = set_memory_encrypted((unsigned long)gpadl->buffer,
885 PFN_UP(gpadl->size));
886 if (ret)
887 pr_warn("Fail to set mem host visibility in GPADL teardown %d.\n", ret);
888
Hank Janssen3e7ee492009-07-13 16:02:34 -0700889 return ret;
890}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700891EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700892
Dexuan Cuid3b26dd2018-08-02 03:08:23 +0000893void vmbus_reset_channel_cb(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700894{
Andrea Parri (Microsoft)9403b662020-04-06 02:15:09 +0200895 unsigned long flags;
896
Dexuan Cui63d55b22015-12-14 16:01:47 -0800897 /*
Dexuan Cuidad72a12017-03-04 18:13:58 -0700898 * vmbus_on_event(), running in the per-channel tasklet, can race
Dexuan Cui63d55b22015-12-14 16:01:47 -0800899 * with vmbus_close_internal() in the case of SMP guest, e.g., when
900 * the former is accessing channel->inbound.ring_buffer, the latter
Dexuan Cuidad72a12017-03-04 18:13:58 -0700901 * could be freeing the ring_buffer pages, so here we must stop it
902 * first.
Andrea Parri (Microsoft)ac504762020-04-06 02:15:07 +0200903 *
904 * vmbus_chan_sched() might call the netvsc driver callback function
905 * that ends up scheduling NAPI work that accesses the ring buffer.
906 * At this point, we have to ensure that any such work is completed
907 * and that the channel ring buffer is no longer being accessed, cf.
908 * the calls to napi_disable() in netvsc_device_remove().
Dexuan Cui63d55b22015-12-14 16:01:47 -0800909 */
Dexuan Cuidad72a12017-03-04 18:13:58 -0700910 tasklet_disable(&channel->callback_event);
Dexuan Cui63d55b22015-12-14 16:01:47 -0800911
Andrea Parri (Microsoft)9403b662020-04-06 02:15:09 +0200912 /* See the inline comments in vmbus_chan_sched(). */
913 spin_lock_irqsave(&channel->sched_lock, flags);
914 channel->onchannel_callback = NULL;
915 spin_unlock_irqrestore(&channel->sched_lock, flags);
Dexuan Cuid3b26dd2018-08-02 03:08:23 +0000916
Andrea Parri (Microsoft)9403b662020-04-06 02:15:09 +0200917 channel->sc_creation_callback = NULL;
Dexuan Cuid3b26dd2018-08-02 03:08:23 +0000918
919 /* Re-enable tasklet for use on re-open */
920 tasklet_enable(&channel->callback_event);
921}
922
923static int vmbus_close_internal(struct vmbus_channel *channel)
924{
925 struct vmbus_channel_close_channel *msg;
926 int ret;
927
928 vmbus_reset_channel_cb(channel);
929
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800930 /*
931 * In case a device driver's probe() fails (e.g.,
932 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
Masahiro Yamada8a1115f2017-03-09 16:16:31 -0800933 * rescinded later (e.g., we dynamically disable an Integrated Service
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800934 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
935 * here we should skip most of the below cleanup work.
936 */
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700937 if (channel->state != CHANNEL_OPENED_STATE)
938 return -EINVAL;
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800939
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700940 channel->state = CHANNEL_OPEN_STATE;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700941
Bill Pemberton454f18a2009-07-27 16:47:24 -0400942 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700943
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700944 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700945
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800946 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
947 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700948
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800949 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
950 true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700951
Vitaly Kuznetsov633b0052017-10-29 12:21:10 -0700952 trace_vmbus_close_internal(msg, ret);
953
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700954 if (ret) {
955 pr_err("Close failed: close post msg return is %d\n", ret);
956 /*
957 * If we failed to post the close msg,
958 * it is perhaps better to leak memory.
959 */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700960 }
961
Bill Pemberton454f18a2009-07-27 16:47:24 -0400962 /* Tear down the gpadl for the channel's ring buffer */
Tianyu Land4dccf32021-10-25 08:21:09 -0400963 else if (channel->ringbuffer_gpadlhandle.gpadl_handle) {
964 ret = vmbus_teardown_gpadl(channel, &channel->ringbuffer_gpadlhandle);
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700965 if (ret) {
966 pr_err("Close failed: teardown gpadl return %d\n", ret);
967 /*
968 * If we failed to teardown gpadl,
969 * it is perhaps better to leak memory.
970 */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700971 }
972 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700973
Andres Beltrane8b7db32020-11-09 11:04:00 +0100974 if (!ret)
975 vmbus_free_requestor(&channel->requestor);
976
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700977 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700978}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700979
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700980/* disconnect ring - close all channels */
981int vmbus_disconnect_ring(struct vmbus_channel *channel)
982{
983 struct vmbus_channel *cur_channel, *tmp;
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700984 int ret;
985
986 if (channel->primary_channel != NULL)
987 return -EINVAL;
988
Dexuan Cuib5679ce2019-01-09 20:56:06 +0000989 list_for_each_entry_safe(cur_channel, tmp, &channel->sc_list, sc_list) {
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700990 if (cur_channel->rescind)
991 wait_for_completion(&cur_channel->rescind_event);
992
993 mutex_lock(&vmbus_connection.channel_mutex);
994 if (vmbus_close_internal(cur_channel) == 0) {
995 vmbus_free_ring(cur_channel);
996
997 if (cur_channel->rescind)
998 hv_process_channel_removal(cur_channel);
999 }
1000 mutex_unlock(&vmbus_connection.channel_mutex);
1001 }
1002
1003 /*
1004 * Now close the primary.
1005 */
1006 mutex_lock(&vmbus_connection.channel_mutex);
1007 ret = vmbus_close_internal(channel);
1008 mutex_unlock(&vmbus_connection.channel_mutex);
1009
1010 return ret;
1011}
1012EXPORT_SYMBOL_GPL(vmbus_disconnect_ring);
1013
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001014/*
1015 * vmbus_close - Close the specified channel
1016 */
1017void vmbus_close(struct vmbus_channel *channel)
1018{
Stephen Hemmingerae6935e2018-09-14 09:10:17 -07001019 if (vmbus_disconnect_ring(channel) == 0)
1020 vmbus_free_ring(channel);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -07001021}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -07001022EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001023
stephen hemminger5dd0fb92017-08-16 08:56:26 -07001024/**
1025 * vmbus_sendpacket() - Send the specified buffer on the given channel
Dexuan Cuife857bb2018-09-23 21:10:41 +00001026 * @channel: Pointer to vmbus_channel structure
1027 * @buffer: Pointer to the buffer you want to send the data from.
1028 * @bufferlen: Maximum size of what the buffer holds.
stephen hemminger5dd0fb92017-08-16 08:56:26 -07001029 * @requestid: Identifier of the request
Dexuan Cuife857bb2018-09-23 21:10:41 +00001030 * @type: Type of packet that is being sent e.g. negotiate, time
1031 * packet etc.
1032 * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
stephen hemminger5dd0fb92017-08-16 08:56:26 -07001033 *
Dexuan Cuife857bb2018-09-23 21:10:41 +00001034 * Sends data in @buffer directly to Hyper-V via the vmbus.
1035 * This will send the data unparsed to Hyper-V.
stephen hemminger5dd0fb92017-08-16 08:56:26 -07001036 *
1037 * Mainly used by Hyper-V drivers.
1038 */
1039int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
1040 u32 bufferlen, u64 requestid,
1041 enum vmbus_packet_type type, u32 flags)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001042{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -07001043 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001044 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +01001045 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -08001046 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001047 u64 aligned_data = 0;
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -07001048 int num_vecs = ((bufferlen != 0) ? 3 : 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001049
Hank Janssen3e7ee492009-07-13 16:02:34 -07001050
Bill Pemberton454f18a2009-07-27 16:47:24 -04001051 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -08001052 desc.type = type; /* VmbusPacketTypeDataInBand; */
1053 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -07001054 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -08001055 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
1056 desc.len8 = (u16)(packetlen_aligned >> 3);
Andres Beltrane8b7db32020-11-09 11:04:00 +01001057 desc.trans_id = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
Hank Janssen3e7ee492009-07-13 16:02:34 -07001058
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -08001059 bufferlist[0].iov_base = &desc;
1060 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
1061 bufferlist[1].iov_base = buffer;
1062 bufferlist[1].iov_len = bufferlen;
1063 bufferlist[2].iov_base = &aligned_data;
1064 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001065
Andres Beltrane8b7db32020-11-09 11:04:00 +01001066 return hv_ringbuffer_write(channel, bufferlist, num_vecs, requestid);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001067}
Haiyang Zhangfff41b22010-10-07 11:40:08 -07001068EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001069
Hank Janssen3e189512010-03-04 22:11:00 +00001070/*
stephen hemminger5a668d82017-08-16 08:56:25 -07001071 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -08001072 * packets using a GPADL Direct packet type. This interface allows you
1073 * to control notifying the host. This will be useful for sending
1074 * batched data. Also the sender can control the send flags
1075 * explicitly.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -07001076 */
stephen hemminger5a668d82017-08-16 08:56:25 -07001077int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
1078 struct hv_page_buffer pagebuffers[],
1079 u32 pagecount, void *buffer, u32 bufferlen,
1080 u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001081{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -07001082 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +00001083 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001084 u32 descsize;
1085 u32 packetlen;
1086 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -08001087 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001088 u64 aligned_data = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001089
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001090 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -04001091 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001092
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -07001093 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +00001094 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -07001095 * largest size we support
1096 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001097 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
1098 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -07001099 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001100 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +01001101 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -07001102
Bill Pemberton454f18a2009-07-27 16:47:24 -04001103 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -08001104 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
stephen hemminger5a668d82017-08-16 08:56:25 -07001105 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -07001106 desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001107 desc.length8 = (u16)(packetlen_aligned >> 3);
Andres Beltrane8b7db32020-11-09 11:04:00 +01001108 desc.transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
Stephen Hemminger33d426a2017-10-05 17:35:05 -07001109 desc.reserved = 0;
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001110 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001111
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001112 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -08001113 desc.range[i].len = pagebuffers[i].len;
1114 desc.range[i].offset = pagebuffers[i].offset;
1115 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001116 }
1117
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -08001118 bufferlist[0].iov_base = &desc;
1119 bufferlist[0].iov_len = descsize;
1120 bufferlist[1].iov_base = buffer;
1121 bufferlist[1].iov_len = bufferlen;
1122 bufferlist[2].iov_base = &aligned_data;
1123 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001124
Andres Beltrane8b7db32020-11-09 11:04:00 +01001125 return hv_ringbuffer_write(channel, bufferlist, 3, requestid);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001126}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -07001127EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001128
Hank Janssen3e189512010-03-04 22:11:00 +00001129/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -07001130 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +00001131 * using a GPADL Direct packet type.
K. Y. Srinivasand61031e2015-01-09 23:54:34 -08001132 * The buffer includes the vmbus descriptor.
1133 */
1134int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
1135 struct vmbus_packet_mpb_array *desc,
1136 u32 desc_size,
1137 void *buffer, u32 bufferlen, u64 requestid)
1138{
K. Y. Srinivasand61031e2015-01-09 23:54:34 -08001139 u32 packetlen;
1140 u32 packetlen_aligned;
1141 struct kvec bufferlist[3];
1142 u64 aligned_data = 0;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -08001143
1144 packetlen = desc_size + bufferlen;
1145 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
1146
1147 /* Setup the descriptor */
1148 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
1149 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -07001150 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
K. Y. Srinivasand61031e2015-01-09 23:54:34 -08001151 desc->length8 = (u16)(packetlen_aligned >> 3);
Andres Beltrane8b7db32020-11-09 11:04:00 +01001152 desc->transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
Stephen Hemminger33d426a2017-10-05 17:35:05 -07001153 desc->reserved = 0;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -08001154 desc->rangecount = 1;
1155
1156 bufferlist[0].iov_base = desc;
1157 bufferlist[0].iov_len = desc_size;
1158 bufferlist[1].iov_base = buffer;
1159 bufferlist[1].iov_len = bufferlen;
1160 bufferlist[2].iov_base = &aligned_data;
1161 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
1162
Andres Beltrane8b7db32020-11-09 11:04:00 +01001163 return hv_ringbuffer_write(channel, bufferlist, 3, requestid);
K. Y. Srinivasand61031e2015-01-09 23:54:34 -08001164}
1165EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
1166
Hank Janssenc88c4e42010-05-04 15:55:05 -07001167/**
Dexuan Cuife857bb2018-09-23 21:10:41 +00001168 * __vmbus_recvpacket() - Retrieve the user packet on the specified channel
1169 * @channel: Pointer to vmbus_channel structure
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001170 * @buffer: Pointer to the buffer you want to receive the data into.
Dexuan Cuife857bb2018-09-23 21:10:41 +00001171 * @bufferlen: Maximum size of what the buffer can hold.
1172 * @buffer_actual_len: The actual size of the data after it was received.
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001173 * @requestid: Identifier of the request
Dexuan Cuife857bb2018-09-23 21:10:41 +00001174 * @raw: true means keep the vmpacket_descriptor header in the received data.
Hank Janssenc88c4e42010-05-04 15:55:05 -07001175 *
1176 * Receives directly from the hyper-v vmbus and puts the data it received
1177 * into Buffer. This will receive the data unparsed from hyper-v.
1178 *
1179 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -07001180 */
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -08001181static inline int
1182__vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
1183 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
1184 bool raw)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001185{
K. Y. Srinivasan33725922016-11-06 13:14:18 -08001186 return hv_ringbuffer_read(channel, buffer, bufferlen,
1187 buffer_actual_len, requestid, raw);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001188
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -08001189}
1190
1191int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
1192 u32 bufferlen, u32 *buffer_actual_len,
1193 u64 *requestid)
1194{
1195 return __vmbus_recvpacket(channel, buffer, bufferlen,
1196 buffer_actual_len, requestid, false);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001197}
Haiyang Zhangfff41b22010-10-07 11:40:08 -07001198EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001199
Hank Janssen3e189512010-03-04 22:11:00 +00001200/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -07001201 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -07001202 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -07001203int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -07001204 u32 bufferlen, u32 *buffer_actual_len,
1205 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001206{
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -08001207 return __vmbus_recvpacket(channel, buffer, bufferlen,
1208 buffer_actual_len, requestid, true);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001209}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -07001210EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
Andres Beltrane8b7db32020-11-09 11:04:00 +01001211
1212/*
1213 * vmbus_next_request_id - Returns a new request id. It is also
1214 * the index at which the guest memory address is stored.
1215 * Uses a spin lock to avoid race conditions.
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001216 * @channel: Pointer to the VMbus channel struct
Andres Beltrane8b7db32020-11-09 11:04:00 +01001217 * @rqst_add: Guest memory address to be stored in the array
1218 */
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001219u64 vmbus_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
Andres Beltrane8b7db32020-11-09 11:04:00 +01001220{
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001221 struct vmbus_requestor *rqstor = &channel->requestor;
Andres Beltrane8b7db32020-11-09 11:04:00 +01001222 unsigned long flags;
1223 u64 current_id;
Andres Beltrane8b7db32020-11-09 11:04:00 +01001224
1225 /* Check rqstor has been initialized */
1226 if (!channel->rqstor_size)
1227 return VMBUS_NO_RQSTOR;
1228
1229 spin_lock_irqsave(&rqstor->req_lock, flags);
1230 current_id = rqstor->next_request_id;
1231
1232 /* Requestor array is full */
1233 if (current_id >= rqstor->size) {
1234 spin_unlock_irqrestore(&rqstor->req_lock, flags);
1235 return VMBUS_RQST_ERROR;
1236 }
1237
1238 rqstor->next_request_id = rqstor->req_arr[current_id];
1239 rqstor->req_arr[current_id] = rqst_addr;
1240
1241 /* The already held spin lock provides atomicity */
1242 bitmap_set(rqstor->req_bitmap, current_id, 1);
1243
1244 spin_unlock_irqrestore(&rqstor->req_lock, flags);
1245
1246 /*
1247 * Cannot return an ID of 0, which is reserved for an unsolicited
1248 * message from Hyper-V.
1249 */
1250 return current_id + 1;
1251}
1252EXPORT_SYMBOL_GPL(vmbus_next_request_id);
1253
1254/*
1255 * vmbus_request_addr - Returns the memory address stored at @trans_id
1256 * in @rqstor. Uses a spin lock to avoid race conditions.
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001257 * @channel: Pointer to the VMbus channel struct
Andres Beltrane8b7db32020-11-09 11:04:00 +01001258 * @trans_id: Request id sent back from Hyper-V. Becomes the requestor's
1259 * next request id.
1260 */
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001261u64 vmbus_request_addr(struct vmbus_channel *channel, u64 trans_id)
Andres Beltrane8b7db32020-11-09 11:04:00 +01001262{
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001263 struct vmbus_requestor *rqstor = &channel->requestor;
Andres Beltrane8b7db32020-11-09 11:04:00 +01001264 unsigned long flags;
1265 u64 req_addr;
Andres Beltrane8b7db32020-11-09 11:04:00 +01001266
1267 /* Check rqstor has been initialized */
1268 if (!channel->rqstor_size)
1269 return VMBUS_NO_RQSTOR;
1270
1271 /* Hyper-V can send an unsolicited message with ID of 0 */
1272 if (!trans_id)
1273 return trans_id;
1274
1275 spin_lock_irqsave(&rqstor->req_lock, flags);
1276
1277 /* Data corresponding to trans_id is stored at trans_id - 1 */
1278 trans_id--;
1279
1280 /* Invalid trans_id */
1281 if (trans_id >= rqstor->size || !test_bit(trans_id, rqstor->req_bitmap)) {
1282 spin_unlock_irqrestore(&rqstor->req_lock, flags);
1283 return VMBUS_RQST_ERROR;
1284 }
1285
1286 req_addr = rqstor->req_arr[trans_id];
1287 rqstor->req_arr[trans_id] = rqstor->next_request_id;
1288 rqstor->next_request_id = trans_id;
1289
1290 /* The already held spin lock provides atomicity */
1291 bitmap_clear(rqstor->req_bitmap, trans_id, 1);
1292
1293 spin_unlock_irqrestore(&rqstor->req_lock, flags);
1294 return req_addr;
1295}
1296EXPORT_SYMBOL_GPL(vmbus_request_addr);