blob: 90070b337c10d856d55e0147361d033ce7d3eeaa [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>
Michael Kelley6ba34172018-08-02 03:08:24 +000020#include <asm/page.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070021
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070022#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070023
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080024#define NUM_PAGES_SPANNED(addr, len) \
25((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
26
Michael Kelley6ba34172018-08-02 03:08:24 +000027static unsigned long virt_to_hvpfn(void *addr)
28{
Dexuan Cuia9fc4342019-05-07 07:46:55 +000029 phys_addr_t paddr;
Michael Kelley6ba34172018-08-02 03:08:24 +000030
31 if (is_vmalloc_addr(addr))
32 paddr = page_to_phys(vmalloc_to_page(addr)) +
33 offset_in_page(addr);
34 else
35 paddr = __pa(addr);
36
37 return paddr >> PAGE_SHIFT;
38}
39
Hank Janssen3e189512010-03-04 22:11:00 +000040/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070041 * vmbus_setevent- Trigger an event notification on the specified
Hank Janssen3e189512010-03-04 22:11:00 +000042 * channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070043 */
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080044void vmbus_setevent(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -070045{
Haiyang Zhang39d70a42010-09-30 10:52:13 -070046 struct hv_monitor_page *monitorpage;
Hank Janssen3e7ee492009-07-13 16:02:34 -070047
Vitaly Kuznetsov991f8f12017-10-29 12:21:16 -070048 trace_vmbus_setevent(channel);
49
K. Y. Srinivasan37242872016-07-01 16:26:37 -070050 /*
51 * For channels marked as in "low latency" mode
52 * bypass the monitor page mechanism.
53 */
Stephen Hemminger5c1bec62017-02-05 17:20:31 -070054 if (channel->offermsg.monitor_allocated && !channel->low_latency) {
55 vmbus_send_interrupt(channel->offermsg.child_relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -070056
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -070057 /* Get the child to parent monitor page */
58 monitorpage = vmbus_connection.monitor_pages[1];
Hank Janssen3e7ee492009-07-13 16:02:34 -070059
Olaf Hering22356582011-03-21 14:41:37 +010060 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080061 (unsigned long *)&monitorpage->trigger_group
62 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -040063
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070064 } else {
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -080065 vmbus_set_event(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -070066 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070067}
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080068EXPORT_SYMBOL_GPL(vmbus_setevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -070069
Stephen Hemmingerae6935e2018-09-14 09:10:17 -070070/* vmbus_free_ring - drop mapping of ring buffer */
71void vmbus_free_ring(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -070072{
Stephen Hemmingerae6935e2018-09-14 09:10:17 -070073 hv_ringbuffer_cleanup(&channel->outbound);
74 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -070075
Stephen Hemmingerae6935e2018-09-14 09:10:17 -070076 if (channel->ringbuffer_page) {
77 __free_pages(channel->ringbuffer_page,
78 get_order(channel->ringbuffer_pagecount
79 << PAGE_SHIFT));
80 channel->ringbuffer_page = NULL;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070081 }
Stephen Hemmingerae6935e2018-09-14 09:10:17 -070082}
83EXPORT_SYMBOL_GPL(vmbus_free_ring);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070084
Stephen Hemmingerae6935e2018-09-14 09:10:17 -070085/* vmbus_alloc_ring - allocate and map pages for ring buffer */
86int vmbus_alloc_ring(struct vmbus_channel *newchannel,
87 u32 send_size, u32 recv_size)
88{
89 struct page *page;
90 int order;
91
92 if (send_size % PAGE_SIZE || recv_size % PAGE_SIZE)
93 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -070094
Bill Pemberton454f18a2009-07-27 16:47:24 -040095 /* Allocate the ring buffer */
Stephen Hemmingerae6935e2018-09-14 09:10:17 -070096 order = get_order(send_size + recv_size);
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070097 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
Stephen Hemminger52a42c22018-09-14 09:10:16 -070098 GFP_KERNEL|__GFP_ZERO, order);
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070099
100 if (!page)
Stephen Hemminger52a42c22018-09-14 09:10:16 -0700101 page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order);
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800102
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700103 if (!page)
104 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700105
Stephen Hemminger52a42c22018-09-14 09:10:16 -0700106 newchannel->ringbuffer_page = page;
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700107 newchannel->ringbuffer_pagecount = (send_size + recv_size) >> PAGE_SHIFT;
108 newchannel->ringbuffer_send_offset = send_size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700109
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700110 return 0;
111}
112EXPORT_SYMBOL_GPL(vmbus_alloc_ring);
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700113
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700114static int __vmbus_open(struct vmbus_channel *newchannel,
115 void *userdata, u32 userdatalen,
116 void (*onchannelcallback)(void *context), void *context)
117{
118 struct vmbus_channel_open_channel *open_msg;
119 struct vmbus_channel_msginfo *open_info = NULL;
120 struct page *page = newchannel->ringbuffer_page;
121 u32 send_pages, recv_pages;
122 unsigned long flags;
123 int err;
124
125 if (userdatalen > MAX_USER_DEFINED_BYTES)
126 return -EINVAL;
127
128 send_pages = newchannel->ringbuffer_send_offset;
129 recv_pages = newchannel->ringbuffer_pagecount - send_pages;
130
131 spin_lock_irqsave(&newchannel->lock, flags);
132 if (newchannel->state != CHANNEL_OPEN_STATE) {
133 spin_unlock_irqrestore(&newchannel->lock, flags);
134 return -EINVAL;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400135 }
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700136 spin_unlock_irqrestore(&newchannel->lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700137
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700138 newchannel->state = CHANNEL_OPENING_STATE;
139 newchannel->onchannel_callback = onchannelcallback;
140 newchannel->channel_callback_context = context;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400141
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700142 err = hv_ringbuffer_init(&newchannel->outbound, page, send_pages);
143 if (err)
144 goto error_clean_ring;
145
146 err = hv_ringbuffer_init(&newchannel->inbound,
147 &page[send_pages], recv_pages);
148 if (err)
149 goto error_clean_ring;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700150
Bill Pemberton454f18a2009-07-27 16:47:24 -0400151 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800152 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700153
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700154 err = vmbus_establish_gpadl(newchannel,
155 page_address(newchannel->ringbuffer_page),
156 (send_pages + recv_pages) << PAGE_SHIFT,
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700157 &newchannel->ringbuffer_gpadlhandle);
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700158 if (err)
159 goto error_clean_ring;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700160
Bill Pemberton454f18a2009-07-27 16:47:24 -0400161 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700162 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700163 sizeof(struct vmbus_channel_open_channel),
164 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700165 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400166 err = -ENOMEM;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700167 goto error_free_gpadl;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400168 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700169
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700170 init_completion(&open_info->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800171 open_info->waiting_channel = newchannel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700172
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700173 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700174 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
175 open_msg->openid = newchannel->offermsg.child_relid;
176 open_msg->child_relid = newchannel->offermsg.child_relid;
177 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700178 open_msg->downstream_ringbuffer_pageoffset = newchannel->ringbuffer_send_offset;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800179 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700180
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700181 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700182 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700183
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800184 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700185 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800186 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800187 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700188
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -0700189 if (newchannel->rescind) {
190 err = -ENODEV;
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700191 goto error_free_info;
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -0700192 }
193
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700194 err = vmbus_post_msg(open_msg,
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800195 sizeof(struct vmbus_channel_open_channel), true);
Hank Janssen98e08702011-03-29 13:58:44 -0700196
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700197 trace_vmbus_open(open_msg, err);
Vitaly Kuznetsov9202d772017-10-29 12:21:09 -0700198
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700199 if (err != 0)
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700200 goto error_clean_msglist;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700201
Vitaly Kuznetsov396e2872016-06-09 17:08:56 -0700202 wait_for_completion(&open_info->waitevent);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800203
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800204 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700205 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800206 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700207
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800208 if (newchannel->rescind) {
209 err = -ENODEV;
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700210 goto error_free_info;
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800211 }
212
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700213 if (open_info->response.open_result.status) {
214 err = -EAGAIN;
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700215 goto error_free_info;
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700216 }
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700217
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700218 newchannel->state = CHANNEL_OPENED_STATE;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700219 kfree(open_info);
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700220 return 0;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400221
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700222error_clean_msglist:
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700223 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
224 list_del(&open_info->msglistentry);
225 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700226error_free_info:
227 kfree(open_info);
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700228error_free_gpadl:
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800229 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700230 newchannel->ringbuffer_gpadlhandle = 0;
231error_clean_ring:
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700232 hv_ringbuffer_cleanup(&newchannel->outbound);
233 hv_ringbuffer_cleanup(&newchannel->inbound);
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800234 newchannel->state = CHANNEL_OPEN_STATE;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400235 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700236}
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700237
238/*
239 * vmbus_connect_ring - Open the channel but reuse ring buffer
240 */
241int vmbus_connect_ring(struct vmbus_channel *newchannel,
242 void (*onchannelcallback)(void *context), void *context)
243{
244 return __vmbus_open(newchannel, NULL, 0, onchannelcallback, context);
245}
246EXPORT_SYMBOL_GPL(vmbus_connect_ring);
247
248/*
249 * vmbus_open - Open the specified channel.
250 */
251int vmbus_open(struct vmbus_channel *newchannel,
252 u32 send_ringbuffer_size, u32 recv_ringbuffer_size,
253 void *userdata, u32 userdatalen,
254 void (*onchannelcallback)(void *context), void *context)
255{
256 int err;
257
258 err = vmbus_alloc_ring(newchannel, send_ringbuffer_size,
259 recv_ringbuffer_size);
260 if (err)
261 return err;
262
263 err = __vmbus_open(newchannel, userdata, userdatalen,
264 onchannelcallback, context);
265 if (err)
266 vmbus_free_ring(newchannel);
267
268 return err;
269}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700270EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700271
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800272/* Used for Hyper-V Socket: a guest client's connect() to the host */
Andy Shevchenko593db802019-01-10 16:25:32 +0200273int vmbus_send_tl_connect_request(const guid_t *shv_guest_servie_id,
274 const guid_t *shv_host_servie_id)
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800275{
276 struct vmbus_channel_tl_connect_request conn_msg;
Vitaly Kuznetsov98f31a02017-10-29 12:21:15 -0700277 int ret;
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800278
279 memset(&conn_msg, 0, sizeof(conn_msg));
280 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
281 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
282 conn_msg.host_service_id = *shv_host_servie_id;
283
Vitaly Kuznetsov98f31a02017-10-29 12:21:15 -0700284 ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
285
286 trace_vmbus_send_tl_connect_request(&conn_msg, ret);
287
288 return ret;
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800289}
290EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
291
Hank Janssen3e189512010-03-04 22:11:00 +0000292/*
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +0200293 * Set/change the vCPU (@target_vp) the channel (@child_relid) will interrupt.
294 *
295 * CHANNELMSG_MODIFYCHANNEL messages are aynchronous. Also, Hyper-V does not
296 * ACK such messages. IOW we can't know when the host will stop interrupting
297 * the "old" vCPU and start interrupting the "new" vCPU for the given channel.
298 *
299 * The CHANNELMSG_MODIFYCHANNEL message type is supported since VMBus version
300 * VERSION_WIN10_V4_1.
301 */
302int vmbus_send_modifychannel(u32 child_relid, u32 target_vp)
303{
304 struct vmbus_channel_modifychannel conn_msg;
305 int ret;
306
307 memset(&conn_msg, 0, sizeof(conn_msg));
308 conn_msg.header.msgtype = CHANNELMSG_MODIFYCHANNEL;
309 conn_msg.child_relid = child_relid;
310 conn_msg.target_vp = target_vp;
311
312 ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
313
314 trace_vmbus_send_modifychannel(&conn_msg, ret);
315
316 return ret;
317}
318EXPORT_SYMBOL_GPL(vmbus_send_modifychannel);
319
320/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700321 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700322 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700323static int create_gpadl_header(void *kbuffer, u32 size,
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700324 struct vmbus_channel_msginfo **msginfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700325{
326 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700327 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700328 struct vmbus_channel_gpadl_header *gpadl_header;
329 struct vmbus_channel_gpadl_body *gpadl_body;
330 struct vmbus_channel_msginfo *msgheader;
331 struct vmbus_channel_msginfo *msgbody = NULL;
332 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700333
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700334 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700335
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700336 pagecount = size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700337
Bill Pemberton454f18a2009-07-27 16:47:24 -0400338 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700339 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700340 sizeof(struct vmbus_channel_gpadl_header) -
341 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700342 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700343
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700344 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700345 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400346 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700347 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700348 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700349 sizeof(struct gpa_range) + pfncount * sizeof(u64);
350 msgheader = kzalloc(msgsize, GFP_KERNEL);
351 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400352 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700353
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800354 INIT_LIST_HEAD(&msgheader->submsglist);
355 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700356
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700357 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800358 msgheader->msg;
359 gpadl_header->rangecount = 1;
360 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700361 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800362 gpadl_header->range[0].byte_offset = 0;
363 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700364 for (i = 0; i < pfncount; i++)
Michael Kelley6ba34172018-08-02 03:08:24 +0000365 gpadl_header->range[0].pfn_array[i] = virt_to_hvpfn(
366 kbuffer + PAGE_SIZE * i);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700367 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700368
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700369 pfnsum = pfncount;
370 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700371
Bill Pemberton454f18a2009-07-27 16:47:24 -0400372 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700373 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700374 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700375 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700376
Bill Pemberton454f18a2009-07-27 16:47:24 -0400377 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700378 while (pfnleft) {
379 if (pfnleft > pfncount)
380 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700381 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700382 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700383
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700384 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700385 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700386 pfncurr * sizeof(u64);
387 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700388
389 if (!msgbody) {
390 struct vmbus_channel_msginfo *pos = NULL;
391 struct vmbus_channel_msginfo *tmp = NULL;
392 /*
393 * Free up all the allocated messages.
394 */
395 list_for_each_entry_safe(pos, tmp,
396 &msgheader->submsglist,
397 msglistentry) {
398
399 list_del(&pos->msglistentry);
400 kfree(pos);
401 }
402
Bill Pembertond1c250b2010-05-05 15:27:35 -0400403 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700404 }
405
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800406 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700407 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800408 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700409
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700410 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700411 * Gpadl is u32 and we are using a pointer which could
412 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700413 * This is governed by the guest/host protocol and
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700414 * so the hypervisor guarantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700415 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700416 for (i = 0; i < pfncurr; i++)
Michael Kelley6ba34172018-08-02 03:08:24 +0000417 gpadl_body->pfn[i] = virt_to_hvpfn(
418 kbuffer + PAGE_SIZE * (pfnsum + i));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700419
Bill Pemberton454f18a2009-07-27 16:47:24 -0400420 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800421 list_add_tail(&msgbody->msglistentry,
422 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700423 pfnsum += pfncurr;
424 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700425 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700426 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400427 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700428 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700429 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700430 sizeof(struct gpa_range) + pagecount * sizeof(u64);
431 msgheader = kzalloc(msgsize, GFP_KERNEL);
432 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400433 goto nomem;
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700434
435 INIT_LIST_HEAD(&msgheader->submsglist);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800436 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700437
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700438 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800439 msgheader->msg;
440 gpadl_header->rangecount = 1;
441 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700442 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800443 gpadl_header->range[0].byte_offset = 0;
444 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700445 for (i = 0; i < pagecount; i++)
Michael Kelley6ba34172018-08-02 03:08:24 +0000446 gpadl_header->range[0].pfn_array[i] = virt_to_hvpfn(
447 kbuffer + PAGE_SIZE * i);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700448
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700449 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700450 }
451
452 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400453nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700454 kfree(msgheader);
455 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400456 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700457}
458
Hank Janssen3e189512010-03-04 22:11:00 +0000459/*
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700460 * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700461 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700462 * @channel: a channel
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800463 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700464 * @size: page-size multiple
465 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700466 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700467int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700468 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700469{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700470 struct vmbus_channel_gpadl_header *gpadlmsg;
471 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700472 struct vmbus_channel_msginfo *msginfo = NULL;
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700473 struct vmbus_channel_msginfo *submsginfo, *tmp;
Bill Pemberton53af5452009-09-11 21:46:44 -0400474 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700475 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700476 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400477 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700478
K. Y. Srinivasan9f52a162015-01-09 23:54:33 -0800479 next_gpadl_handle =
480 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700481
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700482 ret = create_gpadl_header(kbuffer, size, &msginfo);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400483 if (ret)
484 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700485
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700486 init_completion(&msginfo->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800487 msginfo->waiting_channel = channel;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400488
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800489 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
490 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
491 gpadlmsg->child_relid = channel->offermsg.child_relid;
492 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700493
Hank Janssen3e7ee492009-07-13 16:02:34 -0700494
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800495 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800496 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800497 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700498
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800499 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700500
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -0700501 if (channel->rescind) {
502 ret = -ENODEV;
503 goto cleanup;
504 }
505
Haiyang Zhangc6977672011-01-26 12:12:08 -0800506 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800507 sizeof(*msginfo), true);
Vitaly Kuznetsov69edbd52017-10-29 12:21:11 -0700508
509 trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
510
Hank Janssen98e08702011-03-29 13:58:44 -0700511 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700512 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700513
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700514 list_for_each(curr, &msginfo->submsglist) {
515 submsginfo = (struct vmbus_channel_msginfo *)curr;
516 gpadl_body =
517 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Bill Pemberton53af5452009-09-11 21:46:44 -0400518
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700519 gpadl_body->header.msgtype =
520 CHANNELMSG_GPADL_BODY;
521 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700522
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700523 ret = vmbus_post_msg(gpadl_body,
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800524 submsginfo->msgsize - sizeof(*submsginfo),
525 true);
Vitaly Kuznetsov69edbd52017-10-29 12:21:11 -0700526
527 trace_vmbus_establish_gpadl_body(gpadl_body, ret);
528
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700529 if (ret != 0)
530 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700531
Hank Janssen3e7ee492009-07-13 16:02:34 -0700532 }
K. Y. Srinivasan72c6b712014-08-27 16:25:34 -0700533 wait_for_completion(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700534
Dexuan Cuieceb0592018-11-26 02:29:56 +0000535 if (msginfo->response.gpadl_created.creation_status != 0) {
536 pr_err("Failed to establish GPADL: err = 0x%x\n",
537 msginfo->response.gpadl_created.creation_status);
538
539 ret = -EDQUOT;
540 goto cleanup;
541 }
542
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800543 if (channel->rescind) {
544 ret = -ENODEV;
545 goto cleanup;
546 }
547
Bill Pemberton454f18a2009-07-27 16:47:24 -0400548 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800549 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700550
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700551cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800552 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800553 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800554 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700555 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
556 msglistentry) {
557 kfree(submsginfo);
558 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700559
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700560 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700561 return ret;
562}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700563EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700564
Hank Janssen3e189512010-03-04 22:11:00 +0000565/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700566 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700567 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700568int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700569{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700570 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700571 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700572 unsigned long flags;
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700573 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700574
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700575 info = kmalloc(sizeof(*info) +
576 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400577 if (!info)
578 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700579
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700580 init_completion(&info->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800581 info->waiting_channel = channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700582
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800583 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700584
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800585 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
586 msg->child_relid = channel->offermsg.child_relid;
587 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700588
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800589 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800590 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800591 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800592 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -0700593
594 if (channel->rescind)
595 goto post_msg_err;
596
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800597 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
598 true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700599
Vitaly Kuznetsov09cdf8f2017-10-29 12:21:12 -0700600 trace_vmbus_teardown_gpadl(msg, ret);
601
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700602 if (ret)
603 goto post_msg_err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700604
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700605 wait_for_completion(&info->waitevent);
606
607post_msg_err:
K. Y. Srinivasan5e030d52017-03-12 20:00:30 -0700608 /*
609 * If the channel has been rescinded;
610 * we will be awakened by the rescind
611 * handler; set the error code to zero so we don't leak memory.
612 */
613 if (channel->rescind)
614 ret = 0;
615
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800616 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800617 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800618 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700619
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700620 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700621 return ret;
622}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700623EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700624
Dexuan Cuid3b26dd2018-08-02 03:08:23 +0000625void vmbus_reset_channel_cb(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700626{
Andrea Parri (Microsoft)9403b662020-04-06 02:15:09 +0200627 unsigned long flags;
628
Dexuan Cui63d55b22015-12-14 16:01:47 -0800629 /*
Dexuan Cuidad72a12017-03-04 18:13:58 -0700630 * vmbus_on_event(), running in the per-channel tasklet, can race
Dexuan Cui63d55b22015-12-14 16:01:47 -0800631 * with vmbus_close_internal() in the case of SMP guest, e.g., when
632 * the former is accessing channel->inbound.ring_buffer, the latter
Dexuan Cuidad72a12017-03-04 18:13:58 -0700633 * could be freeing the ring_buffer pages, so here we must stop it
634 * first.
Andrea Parri (Microsoft)ac504762020-04-06 02:15:07 +0200635 *
636 * vmbus_chan_sched() might call the netvsc driver callback function
637 * that ends up scheduling NAPI work that accesses the ring buffer.
638 * At this point, we have to ensure that any such work is completed
639 * and that the channel ring buffer is no longer being accessed, cf.
640 * the calls to napi_disable() in netvsc_device_remove().
Dexuan Cui63d55b22015-12-14 16:01:47 -0800641 */
Dexuan Cuidad72a12017-03-04 18:13:58 -0700642 tasklet_disable(&channel->callback_event);
Dexuan Cui63d55b22015-12-14 16:01:47 -0800643
Andrea Parri (Microsoft)9403b662020-04-06 02:15:09 +0200644 /* See the inline comments in vmbus_chan_sched(). */
645 spin_lock_irqsave(&channel->sched_lock, flags);
646 channel->onchannel_callback = NULL;
647 spin_unlock_irqrestore(&channel->sched_lock, flags);
Dexuan Cuid3b26dd2018-08-02 03:08:23 +0000648
Andrea Parri (Microsoft)9403b662020-04-06 02:15:09 +0200649 channel->sc_creation_callback = NULL;
Dexuan Cuid3b26dd2018-08-02 03:08:23 +0000650
651 /* Re-enable tasklet for use on re-open */
652 tasklet_enable(&channel->callback_event);
653}
654
655static int vmbus_close_internal(struct vmbus_channel *channel)
656{
657 struct vmbus_channel_close_channel *msg;
658 int ret;
659
660 vmbus_reset_channel_cb(channel);
661
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800662 /*
663 * In case a device driver's probe() fails (e.g.,
664 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
Masahiro Yamada8a1115f2017-03-09 16:16:31 -0800665 * rescinded later (e.g., we dynamically disable an Integrated Service
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800666 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
667 * here we should skip most of the below cleanup work.
668 */
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700669 if (channel->state != CHANNEL_OPENED_STATE)
670 return -EINVAL;
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800671
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700672 channel->state = CHANNEL_OPEN_STATE;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700673
Bill Pemberton454f18a2009-07-27 16:47:24 -0400674 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700675
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700676 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700677
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800678 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
679 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700680
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800681 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
682 true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700683
Vitaly Kuznetsov633b0052017-10-29 12:21:10 -0700684 trace_vmbus_close_internal(msg, ret);
685
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700686 if (ret) {
687 pr_err("Close failed: close post msg return is %d\n", ret);
688 /*
689 * If we failed to post the close msg,
690 * it is perhaps better to leak memory.
691 */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700692 }
693
Bill Pemberton454f18a2009-07-27 16:47:24 -0400694 /* Tear down the gpadl for the channel's ring buffer */
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700695 else if (channel->ringbuffer_gpadlhandle) {
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700696 ret = vmbus_teardown_gpadl(channel,
697 channel->ringbuffer_gpadlhandle);
698 if (ret) {
699 pr_err("Close failed: teardown gpadl return %d\n", ret);
700 /*
701 * If we failed to teardown gpadl,
702 * it is perhaps better to leak memory.
703 */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700704 }
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700705
706 channel->ringbuffer_gpadlhandle = 0;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700707 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700708
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700709 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700710}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700711
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700712/* disconnect ring - close all channels */
713int vmbus_disconnect_ring(struct vmbus_channel *channel)
714{
715 struct vmbus_channel *cur_channel, *tmp;
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700716 int ret;
717
718 if (channel->primary_channel != NULL)
719 return -EINVAL;
720
Dexuan Cuib5679ce2019-01-09 20:56:06 +0000721 list_for_each_entry_safe(cur_channel, tmp, &channel->sc_list, sc_list) {
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700722 if (cur_channel->rescind)
723 wait_for_completion(&cur_channel->rescind_event);
724
725 mutex_lock(&vmbus_connection.channel_mutex);
726 if (vmbus_close_internal(cur_channel) == 0) {
727 vmbus_free_ring(cur_channel);
728
729 if (cur_channel->rescind)
730 hv_process_channel_removal(cur_channel);
731 }
732 mutex_unlock(&vmbus_connection.channel_mutex);
733 }
734
735 /*
736 * Now close the primary.
737 */
738 mutex_lock(&vmbus_connection.channel_mutex);
739 ret = vmbus_close_internal(channel);
740 mutex_unlock(&vmbus_connection.channel_mutex);
741
742 return ret;
743}
744EXPORT_SYMBOL_GPL(vmbus_disconnect_ring);
745
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700746/*
747 * vmbus_close - Close the specified channel
748 */
749void vmbus_close(struct vmbus_channel *channel)
750{
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700751 if (vmbus_disconnect_ring(channel) == 0)
752 vmbus_free_ring(channel);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700753}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700754EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700755
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700756/**
757 * vmbus_sendpacket() - Send the specified buffer on the given channel
Dexuan Cuife857bb2018-09-23 21:10:41 +0000758 * @channel: Pointer to vmbus_channel structure
759 * @buffer: Pointer to the buffer you want to send the data from.
760 * @bufferlen: Maximum size of what the buffer holds.
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700761 * @requestid: Identifier of the request
Dexuan Cuife857bb2018-09-23 21:10:41 +0000762 * @type: Type of packet that is being sent e.g. negotiate, time
763 * packet etc.
764 * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700765 *
Dexuan Cuife857bb2018-09-23 21:10:41 +0000766 * Sends data in @buffer directly to Hyper-V via the vmbus.
767 * This will send the data unparsed to Hyper-V.
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700768 *
769 * Mainly used by Hyper-V drivers.
770 */
771int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
772 u32 bufferlen, u64 requestid,
773 enum vmbus_packet_type type, u32 flags)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700774{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700775 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700776 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100777 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800778 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700779 u64 aligned_data = 0;
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700780 int num_vecs = ((bufferlen != 0) ? 3 : 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700781
Hank Janssen3e7ee492009-07-13 16:02:34 -0700782
Bill Pemberton454f18a2009-07-27 16:47:24 -0400783 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800784 desc.type = type; /* VmbusPacketTypeDataInBand; */
785 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700786 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800787 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
788 desc.len8 = (u16)(packetlen_aligned >> 3);
789 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700790
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800791 bufferlist[0].iov_base = &desc;
792 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
793 bufferlist[1].iov_base = buffer;
794 bufferlist[1].iov_len = bufferlen;
795 bufferlist[2].iov_base = &aligned_data;
796 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700797
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700798 return hv_ringbuffer_write(channel, bufferlist, num_vecs);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700799}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700800EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700801
Hank Janssen3e189512010-03-04 22:11:00 +0000802/*
stephen hemminger5a668d82017-08-16 08:56:25 -0700803 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800804 * packets using a GPADL Direct packet type. This interface allows you
805 * to control notifying the host. This will be useful for sending
806 * batched data. Also the sender can control the send flags
807 * explicitly.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700808 */
stephen hemminger5a668d82017-08-16 08:56:25 -0700809int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
810 struct hv_page_buffer pagebuffers[],
811 u32 pagecount, void *buffer, u32 bufferlen,
812 u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700813{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700814 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000815 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700816 u32 descsize;
817 u32 packetlen;
818 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800819 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700820 u64 aligned_data = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700821
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700822 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400823 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700824
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700825 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000826 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700827 * largest size we support
828 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700829 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
830 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700831 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700832 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100833 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700834
Bill Pemberton454f18a2009-07-27 16:47:24 -0400835 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800836 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
stephen hemminger5a668d82017-08-16 08:56:25 -0700837 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700838 desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700839 desc.length8 = (u16)(packetlen_aligned >> 3);
840 desc.transactionid = requestid;
Stephen Hemminger33d426a2017-10-05 17:35:05 -0700841 desc.reserved = 0;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700842 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700843
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700844 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800845 desc.range[i].len = pagebuffers[i].len;
846 desc.range[i].offset = pagebuffers[i].offset;
847 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700848 }
849
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800850 bufferlist[0].iov_base = &desc;
851 bufferlist[0].iov_len = descsize;
852 bufferlist[1].iov_base = buffer;
853 bufferlist[1].iov_len = bufferlen;
854 bufferlist[2].iov_base = &aligned_data;
855 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700856
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700857 return hv_ringbuffer_write(channel, bufferlist, 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700858}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700859EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700860
Hank Janssen3e189512010-03-04 22:11:00 +0000861/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700862 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000863 * using a GPADL Direct packet type.
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800864 * The buffer includes the vmbus descriptor.
865 */
866int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
867 struct vmbus_packet_mpb_array *desc,
868 u32 desc_size,
869 void *buffer, u32 bufferlen, u64 requestid)
870{
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800871 u32 packetlen;
872 u32 packetlen_aligned;
873 struct kvec bufferlist[3];
874 u64 aligned_data = 0;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800875
876 packetlen = desc_size + bufferlen;
877 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
878
879 /* Setup the descriptor */
880 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
881 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700882 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800883 desc->length8 = (u16)(packetlen_aligned >> 3);
884 desc->transactionid = requestid;
Stephen Hemminger33d426a2017-10-05 17:35:05 -0700885 desc->reserved = 0;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800886 desc->rangecount = 1;
887
888 bufferlist[0].iov_base = desc;
889 bufferlist[0].iov_len = desc_size;
890 bufferlist[1].iov_base = buffer;
891 bufferlist[1].iov_len = bufferlen;
892 bufferlist[2].iov_base = &aligned_data;
893 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
894
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700895 return hv_ringbuffer_write(channel, bufferlist, 3);
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800896}
897EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
898
Hank Janssenc88c4e42010-05-04 15:55:05 -0700899/**
Dexuan Cuife857bb2018-09-23 21:10:41 +0000900 * __vmbus_recvpacket() - Retrieve the user packet on the specified channel
901 * @channel: Pointer to vmbus_channel structure
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700902 * @buffer: Pointer to the buffer you want to receive the data into.
Dexuan Cuife857bb2018-09-23 21:10:41 +0000903 * @bufferlen: Maximum size of what the buffer can hold.
904 * @buffer_actual_len: The actual size of the data after it was received.
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700905 * @requestid: Identifier of the request
Dexuan Cuife857bb2018-09-23 21:10:41 +0000906 * @raw: true means keep the vmpacket_descriptor header in the received data.
Hank Janssenc88c4e42010-05-04 15:55:05 -0700907 *
908 * Receives directly from the hyper-v vmbus and puts the data it received
909 * into Buffer. This will receive the data unparsed from hyper-v.
910 *
911 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700912 */
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800913static inline int
914__vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
915 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
916 bool raw)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700917{
K. Y. Srinivasan33725922016-11-06 13:14:18 -0800918 return hv_ringbuffer_read(channel, buffer, bufferlen,
919 buffer_actual_len, requestid, raw);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700920
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800921}
922
923int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
924 u32 bufferlen, u32 *buffer_actual_len,
925 u64 *requestid)
926{
927 return __vmbus_recvpacket(channel, buffer, bufferlen,
928 buffer_actual_len, requestid, false);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700929}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700930EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700931
Hank Janssen3e189512010-03-04 22:11:00 +0000932/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700933 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700934 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700935int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700936 u32 bufferlen, u32 *buffer_actual_len,
937 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700938{
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800939 return __vmbus_recvpacket(channel, buffer, bufferlen,
940 buffer_actual_len, requestid, true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700941}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700942EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);