blob: 23f358cb7f494c159e2ca9d74046b470b75673a1 [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/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700293 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700294 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700295static int create_gpadl_header(void *kbuffer, u32 size,
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700296 struct vmbus_channel_msginfo **msginfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700297{
298 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700299 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700300 struct vmbus_channel_gpadl_header *gpadl_header;
301 struct vmbus_channel_gpadl_body *gpadl_body;
302 struct vmbus_channel_msginfo *msgheader;
303 struct vmbus_channel_msginfo *msgbody = NULL;
304 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700305
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700306 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700307
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700308 pagecount = size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700309
Bill Pemberton454f18a2009-07-27 16:47:24 -0400310 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700311 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700312 sizeof(struct vmbus_channel_gpadl_header) -
313 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700314 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700315
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700316 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700317 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400318 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700319 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700320 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700321 sizeof(struct gpa_range) + pfncount * sizeof(u64);
322 msgheader = kzalloc(msgsize, GFP_KERNEL);
323 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400324 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700325
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800326 INIT_LIST_HEAD(&msgheader->submsglist);
327 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700328
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700329 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800330 msgheader->msg;
331 gpadl_header->rangecount = 1;
332 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700333 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800334 gpadl_header->range[0].byte_offset = 0;
335 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700336 for (i = 0; i < pfncount; i++)
Michael Kelley6ba34172018-08-02 03:08:24 +0000337 gpadl_header->range[0].pfn_array[i] = virt_to_hvpfn(
338 kbuffer + PAGE_SIZE * i);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700339 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700340
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700341 pfnsum = pfncount;
342 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700343
Bill Pemberton454f18a2009-07-27 16:47:24 -0400344 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700345 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700346 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700347 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700348
Bill Pemberton454f18a2009-07-27 16:47:24 -0400349 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700350 while (pfnleft) {
351 if (pfnleft > pfncount)
352 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700353 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700354 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700355
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700356 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700357 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700358 pfncurr * sizeof(u64);
359 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700360
361 if (!msgbody) {
362 struct vmbus_channel_msginfo *pos = NULL;
363 struct vmbus_channel_msginfo *tmp = NULL;
364 /*
365 * Free up all the allocated messages.
366 */
367 list_for_each_entry_safe(pos, tmp,
368 &msgheader->submsglist,
369 msglistentry) {
370
371 list_del(&pos->msglistentry);
372 kfree(pos);
373 }
374
Bill Pembertond1c250b2010-05-05 15:27:35 -0400375 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700376 }
377
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800378 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700379 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800380 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700381
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700382 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700383 * Gpadl is u32 and we are using a pointer which could
384 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700385 * This is governed by the guest/host protocol and
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700386 * so the hypervisor guarantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700387 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700388 for (i = 0; i < pfncurr; i++)
Michael Kelley6ba34172018-08-02 03:08:24 +0000389 gpadl_body->pfn[i] = virt_to_hvpfn(
390 kbuffer + PAGE_SIZE * (pfnsum + i));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700391
Bill Pemberton454f18a2009-07-27 16:47:24 -0400392 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800393 list_add_tail(&msgbody->msglistentry,
394 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700395 pfnsum += pfncurr;
396 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700397 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700398 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400399 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700400 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700401 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700402 sizeof(struct gpa_range) + pagecount * sizeof(u64);
403 msgheader = kzalloc(msgsize, GFP_KERNEL);
404 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400405 goto nomem;
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700406
407 INIT_LIST_HEAD(&msgheader->submsglist);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800408 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700409
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700410 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800411 msgheader->msg;
412 gpadl_header->rangecount = 1;
413 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700414 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800415 gpadl_header->range[0].byte_offset = 0;
416 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700417 for (i = 0; i < pagecount; i++)
Michael Kelley6ba34172018-08-02 03:08:24 +0000418 gpadl_header->range[0].pfn_array[i] = virt_to_hvpfn(
419 kbuffer + PAGE_SIZE * i);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700420
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700421 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700422 }
423
424 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400425nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700426 kfree(msgheader);
427 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400428 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700429}
430
Hank Janssen3e189512010-03-04 22:11:00 +0000431/*
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700432 * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700433 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700434 * @channel: a channel
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800435 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700436 * @size: page-size multiple
437 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700438 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700439int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700440 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700441{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700442 struct vmbus_channel_gpadl_header *gpadlmsg;
443 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700444 struct vmbus_channel_msginfo *msginfo = NULL;
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700445 struct vmbus_channel_msginfo *submsginfo, *tmp;
Bill Pemberton53af5452009-09-11 21:46:44 -0400446 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700447 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700448 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400449 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700450
K. Y. Srinivasan9f52a162015-01-09 23:54:33 -0800451 next_gpadl_handle =
452 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700453
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700454 ret = create_gpadl_header(kbuffer, size, &msginfo);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400455 if (ret)
456 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700457
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700458 init_completion(&msginfo->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800459 msginfo->waiting_channel = channel;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400460
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800461 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
462 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
463 gpadlmsg->child_relid = channel->offermsg.child_relid;
464 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700465
Hank Janssen3e7ee492009-07-13 16:02:34 -0700466
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800467 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800468 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800469 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700470
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800471 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700472
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -0700473 if (channel->rescind) {
474 ret = -ENODEV;
475 goto cleanup;
476 }
477
Haiyang Zhangc6977672011-01-26 12:12:08 -0800478 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800479 sizeof(*msginfo), true);
Vitaly Kuznetsov69edbd52017-10-29 12:21:11 -0700480
481 trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
482
Hank Janssen98e08702011-03-29 13:58:44 -0700483 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700484 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700485
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700486 list_for_each(curr, &msginfo->submsglist) {
487 submsginfo = (struct vmbus_channel_msginfo *)curr;
488 gpadl_body =
489 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Bill Pemberton53af5452009-09-11 21:46:44 -0400490
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700491 gpadl_body->header.msgtype =
492 CHANNELMSG_GPADL_BODY;
493 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700494
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700495 ret = vmbus_post_msg(gpadl_body,
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800496 submsginfo->msgsize - sizeof(*submsginfo),
497 true);
Vitaly Kuznetsov69edbd52017-10-29 12:21:11 -0700498
499 trace_vmbus_establish_gpadl_body(gpadl_body, ret);
500
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700501 if (ret != 0)
502 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700503
Hank Janssen3e7ee492009-07-13 16:02:34 -0700504 }
K. Y. Srinivasan72c6b712014-08-27 16:25:34 -0700505 wait_for_completion(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700506
Dexuan Cuieceb0592018-11-26 02:29:56 +0000507 if (msginfo->response.gpadl_created.creation_status != 0) {
508 pr_err("Failed to establish GPADL: err = 0x%x\n",
509 msginfo->response.gpadl_created.creation_status);
510
511 ret = -EDQUOT;
512 goto cleanup;
513 }
514
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800515 if (channel->rescind) {
516 ret = -ENODEV;
517 goto cleanup;
518 }
519
Bill Pemberton454f18a2009-07-27 16:47:24 -0400520 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800521 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700522
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700523cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800524 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800525 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800526 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700527 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
528 msglistentry) {
529 kfree(submsginfo);
530 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700531
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700532 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700533 return ret;
534}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700535EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700536
Hank Janssen3e189512010-03-04 22:11:00 +0000537/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700538 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700539 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700540int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700541{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700542 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700543 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700544 unsigned long flags;
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700545 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700546
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700547 info = kmalloc(sizeof(*info) +
548 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400549 if (!info)
550 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700551
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700552 init_completion(&info->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800553 info->waiting_channel = channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700554
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800555 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700556
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800557 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
558 msg->child_relid = channel->offermsg.child_relid;
559 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700560
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800561 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800562 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800563 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800564 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -0700565
566 if (channel->rescind)
567 goto post_msg_err;
568
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800569 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
570 true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700571
Vitaly Kuznetsov09cdf8f2017-10-29 12:21:12 -0700572 trace_vmbus_teardown_gpadl(msg, ret);
573
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700574 if (ret)
575 goto post_msg_err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700576
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700577 wait_for_completion(&info->waitevent);
578
579post_msg_err:
K. Y. Srinivasan5e030d52017-03-12 20:00:30 -0700580 /*
581 * If the channel has been rescinded;
582 * we will be awakened by the rescind
583 * handler; set the error code to zero so we don't leak memory.
584 */
585 if (channel->rescind)
586 ret = 0;
587
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800588 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800589 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800590 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700591
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700592 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700593 return ret;
594}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700595EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700596
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700597static void reset_channel_cb(void *arg)
598{
599 struct vmbus_channel *channel = arg;
600
601 channel->onchannel_callback = NULL;
602}
603
Dexuan Cuid3b26dd2018-08-02 03:08:23 +0000604void vmbus_reset_channel_cb(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700605{
Dexuan Cui63d55b22015-12-14 16:01:47 -0800606 /*
Dexuan Cuidad72a12017-03-04 18:13:58 -0700607 * vmbus_on_event(), running in the per-channel tasklet, can race
Dexuan Cui63d55b22015-12-14 16:01:47 -0800608 * with vmbus_close_internal() in the case of SMP guest, e.g., when
609 * the former is accessing channel->inbound.ring_buffer, the latter
Dexuan Cuidad72a12017-03-04 18:13:58 -0700610 * could be freeing the ring_buffer pages, so here we must stop it
611 * first.
Dexuan Cui63d55b22015-12-14 16:01:47 -0800612 */
Dexuan Cuidad72a12017-03-04 18:13:58 -0700613 tasklet_disable(&channel->callback_event);
Dexuan Cui63d55b22015-12-14 16:01:47 -0800614
Dexuan Cuid3b26dd2018-08-02 03:08:23 +0000615 channel->sc_creation_callback = NULL;
616
617 /* Stop the callback asap */
618 if (channel->target_cpu != get_cpu()) {
619 put_cpu();
620 smp_call_function_single(channel->target_cpu, reset_channel_cb,
621 channel, true);
622 } else {
623 reset_channel_cb(channel);
624 put_cpu();
625 }
626
627 /* Re-enable tasklet for use on re-open */
628 tasklet_enable(&channel->callback_event);
629}
630
631static int vmbus_close_internal(struct vmbus_channel *channel)
632{
633 struct vmbus_channel_close_channel *msg;
634 int ret;
635
636 vmbus_reset_channel_cb(channel);
637
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800638 /*
639 * In case a device driver's probe() fails (e.g.,
640 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
Masahiro Yamada8a1115f2017-03-09 16:16:31 -0800641 * rescinded later (e.g., we dynamically disable an Integrated Service
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800642 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
643 * here we should skip most of the below cleanup work.
644 */
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700645 if (channel->state != CHANNEL_OPENED_STATE)
646 return -EINVAL;
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800647
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700648 channel->state = CHANNEL_OPEN_STATE;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700649
Bill Pemberton454f18a2009-07-27 16:47:24 -0400650 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700651
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700652 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700653
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800654 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
655 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700656
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800657 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
658 true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700659
Vitaly Kuznetsov633b0052017-10-29 12:21:10 -0700660 trace_vmbus_close_internal(msg, ret);
661
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700662 if (ret) {
663 pr_err("Close failed: close post msg return is %d\n", ret);
664 /*
665 * If we failed to post the close msg,
666 * it is perhaps better to leak memory.
667 */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700668 }
669
Bill Pemberton454f18a2009-07-27 16:47:24 -0400670 /* Tear down the gpadl for the channel's ring buffer */
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700671 else if (channel->ringbuffer_gpadlhandle) {
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700672 ret = vmbus_teardown_gpadl(channel,
673 channel->ringbuffer_gpadlhandle);
674 if (ret) {
675 pr_err("Close failed: teardown gpadl return %d\n", ret);
676 /*
677 * If we failed to teardown gpadl,
678 * it is perhaps better to leak memory.
679 */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700680 }
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700681
682 channel->ringbuffer_gpadlhandle = 0;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700683 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700684
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700685 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700686}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700687
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700688/* disconnect ring - close all channels */
689int vmbus_disconnect_ring(struct vmbus_channel *channel)
690{
691 struct vmbus_channel *cur_channel, *tmp;
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700692 int ret;
693
694 if (channel->primary_channel != NULL)
695 return -EINVAL;
696
Dexuan Cuib5679ce2019-01-09 20:56:06 +0000697 list_for_each_entry_safe(cur_channel, tmp, &channel->sc_list, sc_list) {
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700698 if (cur_channel->rescind)
699 wait_for_completion(&cur_channel->rescind_event);
700
701 mutex_lock(&vmbus_connection.channel_mutex);
702 if (vmbus_close_internal(cur_channel) == 0) {
703 vmbus_free_ring(cur_channel);
704
705 if (cur_channel->rescind)
706 hv_process_channel_removal(cur_channel);
707 }
708 mutex_unlock(&vmbus_connection.channel_mutex);
709 }
710
711 /*
712 * Now close the primary.
713 */
714 mutex_lock(&vmbus_connection.channel_mutex);
715 ret = vmbus_close_internal(channel);
716 mutex_unlock(&vmbus_connection.channel_mutex);
717
718 return ret;
719}
720EXPORT_SYMBOL_GPL(vmbus_disconnect_ring);
721
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700722/*
723 * vmbus_close - Close the specified channel
724 */
725void vmbus_close(struct vmbus_channel *channel)
726{
Stephen Hemmingerae6935e2018-09-14 09:10:17 -0700727 if (vmbus_disconnect_ring(channel) == 0)
728 vmbus_free_ring(channel);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700729}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700730EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700731
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700732/**
733 * vmbus_sendpacket() - Send the specified buffer on the given channel
Dexuan Cuife857bb2018-09-23 21:10:41 +0000734 * @channel: Pointer to vmbus_channel structure
735 * @buffer: Pointer to the buffer you want to send the data from.
736 * @bufferlen: Maximum size of what the buffer holds.
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700737 * @requestid: Identifier of the request
Dexuan Cuife857bb2018-09-23 21:10:41 +0000738 * @type: Type of packet that is being sent e.g. negotiate, time
739 * packet etc.
740 * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700741 *
Dexuan Cuife857bb2018-09-23 21:10:41 +0000742 * Sends data in @buffer directly to Hyper-V via the vmbus.
743 * This will send the data unparsed to Hyper-V.
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700744 *
745 * Mainly used by Hyper-V drivers.
746 */
747int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
748 u32 bufferlen, u64 requestid,
749 enum vmbus_packet_type type, u32 flags)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700750{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700751 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700752 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100753 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800754 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700755 u64 aligned_data = 0;
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700756 int num_vecs = ((bufferlen != 0) ? 3 : 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700757
Hank Janssen3e7ee492009-07-13 16:02:34 -0700758
Bill Pemberton454f18a2009-07-27 16:47:24 -0400759 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800760 desc.type = type; /* VmbusPacketTypeDataInBand; */
761 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700762 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800763 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
764 desc.len8 = (u16)(packetlen_aligned >> 3);
765 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700766
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800767 bufferlist[0].iov_base = &desc;
768 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
769 bufferlist[1].iov_base = buffer;
770 bufferlist[1].iov_len = bufferlen;
771 bufferlist[2].iov_base = &aligned_data;
772 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700773
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700774 return hv_ringbuffer_write(channel, bufferlist, num_vecs);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700775}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700776EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700777
Hank Janssen3e189512010-03-04 22:11:00 +0000778/*
stephen hemminger5a668d82017-08-16 08:56:25 -0700779 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800780 * packets using a GPADL Direct packet type. This interface allows you
781 * to control notifying the host. This will be useful for sending
782 * batched data. Also the sender can control the send flags
783 * explicitly.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700784 */
stephen hemminger5a668d82017-08-16 08:56:25 -0700785int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
786 struct hv_page_buffer pagebuffers[],
787 u32 pagecount, void *buffer, u32 bufferlen,
788 u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700789{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700790 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000791 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700792 u32 descsize;
793 u32 packetlen;
794 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800795 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700796 u64 aligned_data = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700797
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700798 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400799 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700800
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700801 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000802 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700803 * largest size we support
804 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700805 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
806 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700807 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700808 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100809 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700810
Bill Pemberton454f18a2009-07-27 16:47:24 -0400811 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800812 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
stephen hemminger5a668d82017-08-16 08:56:25 -0700813 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700814 desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700815 desc.length8 = (u16)(packetlen_aligned >> 3);
816 desc.transactionid = requestid;
Stephen Hemminger33d426a2017-10-05 17:35:05 -0700817 desc.reserved = 0;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700818 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700819
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700820 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800821 desc.range[i].len = pagebuffers[i].len;
822 desc.range[i].offset = pagebuffers[i].offset;
823 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700824 }
825
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800826 bufferlist[0].iov_base = &desc;
827 bufferlist[0].iov_len = descsize;
828 bufferlist[1].iov_base = buffer;
829 bufferlist[1].iov_len = bufferlen;
830 bufferlist[2].iov_base = &aligned_data;
831 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700832
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700833 return hv_ringbuffer_write(channel, bufferlist, 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700834}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700835EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700836
Hank Janssen3e189512010-03-04 22:11:00 +0000837/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700838 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000839 * using a GPADL Direct packet type.
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800840 * The buffer includes the vmbus descriptor.
841 */
842int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
843 struct vmbus_packet_mpb_array *desc,
844 u32 desc_size,
845 void *buffer, u32 bufferlen, u64 requestid)
846{
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800847 u32 packetlen;
848 u32 packetlen_aligned;
849 struct kvec bufferlist[3];
850 u64 aligned_data = 0;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800851
852 packetlen = desc_size + bufferlen;
853 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
854
855 /* Setup the descriptor */
856 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
857 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700858 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800859 desc->length8 = (u16)(packetlen_aligned >> 3);
860 desc->transactionid = requestid;
Stephen Hemminger33d426a2017-10-05 17:35:05 -0700861 desc->reserved = 0;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800862 desc->rangecount = 1;
863
864 bufferlist[0].iov_base = desc;
865 bufferlist[0].iov_len = desc_size;
866 bufferlist[1].iov_base = buffer;
867 bufferlist[1].iov_len = bufferlen;
868 bufferlist[2].iov_base = &aligned_data;
869 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
870
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700871 return hv_ringbuffer_write(channel, bufferlist, 3);
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800872}
873EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
874
Hank Janssenc88c4e42010-05-04 15:55:05 -0700875/**
Dexuan Cuife857bb2018-09-23 21:10:41 +0000876 * __vmbus_recvpacket() - Retrieve the user packet on the specified channel
877 * @channel: Pointer to vmbus_channel structure
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700878 * @buffer: Pointer to the buffer you want to receive the data into.
Dexuan Cuife857bb2018-09-23 21:10:41 +0000879 * @bufferlen: Maximum size of what the buffer can hold.
880 * @buffer_actual_len: The actual size of the data after it was received.
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700881 * @requestid: Identifier of the request
Dexuan Cuife857bb2018-09-23 21:10:41 +0000882 * @raw: true means keep the vmpacket_descriptor header in the received data.
Hank Janssenc88c4e42010-05-04 15:55:05 -0700883 *
884 * Receives directly from the hyper-v vmbus and puts the data it received
885 * into Buffer. This will receive the data unparsed from hyper-v.
886 *
887 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700888 */
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800889static inline int
890__vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
891 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
892 bool raw)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700893{
K. Y. Srinivasan33725922016-11-06 13:14:18 -0800894 return hv_ringbuffer_read(channel, buffer, bufferlen,
895 buffer_actual_len, requestid, raw);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700896
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800897}
898
899int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
900 u32 bufferlen, u32 *buffer_actual_len,
901 u64 *requestid)
902{
903 return __vmbus_recvpacket(channel, buffer, bufferlen,
904 buffer_actual_len, requestid, false);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700905}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700906EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700907
Hank Janssen3e189512010-03-04 22:11:00 +0000908/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700909 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700910 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700911int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700912 u32 bufferlen, u32 *buffer_actual_len,
913 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700914{
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800915 return __vmbus_recvpacket(channel, buffer, bufferlen,
916 buffer_actual_len, requestid, true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700917}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700918EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);