blob: ba0a092ae085d64e309ec9c5b19a5d80d6372a93 [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
Hank Janssen3e7ee492009-07-13 16:02:34 -07002 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssen3e7ee492009-07-13 16:02:34 -070020 */
Hank Janssen0a466182011-03-29 13:58:47 -070021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Greg Kroah-Hartman5654e932009-07-14 15:08:20 -070023#include <linux/kernel.h>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080024#include <linux/sched.h>
25#include <linux/wait.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070026#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Hank Janssenc88c4e42010-05-04 15:55:05 -070028#include <linux/module.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070029#include <linux/hyperv.h>
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -080030#include <linux/uio.h>
Dexuan Cui63d55b22015-12-14 16:01:47 -080031#include <linux/interrupt.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070032
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070033#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070034
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080035#define NUM_PAGES_SPANNED(addr, len) \
36((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
37
Hank Janssen3e189512010-03-04 22:11:00 +000038/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070039 * vmbus_setevent- Trigger an event notification on the specified
Hank Janssen3e189512010-03-04 22:11:00 +000040 * channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070041 */
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080042void vmbus_setevent(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -070043{
Haiyang Zhang39d70a42010-09-30 10:52:13 -070044 struct hv_monitor_page *monitorpage;
Hank Janssen3e7ee492009-07-13 16:02:34 -070045
Vitaly Kuznetsov991f8f12017-10-29 12:21:16 -070046 trace_vmbus_setevent(channel);
47
K. Y. Srinivasan37242872016-07-01 16:26:37 -070048 /*
49 * For channels marked as in "low latency" mode
50 * bypass the monitor page mechanism.
51 */
Stephen Hemminger5c1bec62017-02-05 17:20:31 -070052 if (channel->offermsg.monitor_allocated && !channel->low_latency) {
53 vmbus_send_interrupt(channel->offermsg.child_relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -070054
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -070055 /* Get the child to parent monitor page */
56 monitorpage = vmbus_connection.monitor_pages[1];
Hank Janssen3e7ee492009-07-13 16:02:34 -070057
Olaf Hering22356582011-03-21 14:41:37 +010058 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080059 (unsigned long *)&monitorpage->trigger_group
60 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -040061
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070062 } else {
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -080063 vmbus_set_event(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -070064 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070065}
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080066EXPORT_SYMBOL_GPL(vmbus_setevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -070067
Hank Janssen3e189512010-03-04 22:11:00 +000068/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070069 * vmbus_open - Open the specified channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070070 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070071int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -070072 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
73 void (*onchannelcallback)(void *context), void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -070074{
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -070075 struct vmbus_channel_open_channel *open_msg;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -070076 struct vmbus_channel_msginfo *open_info = NULL;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070077 unsigned long flags;
Nicholas Mc Guire08a95132015-02-27 11:26:01 -080078 int ret, err = 0;
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070079 struct page *page;
Hank Janssen3e7ee492009-07-13 16:02:34 -070080
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -070081 if (send_ringbuffer_size % PAGE_SIZE ||
82 recv_ringbuffer_size % PAGE_SIZE)
83 return -EINVAL;
84
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010085 spin_lock_irqsave(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070086 if (newchannel->state == CHANNEL_OPEN_STATE) {
87 newchannel->state = CHANNEL_OPENING_STATE;
88 } else {
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010089 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070090 return -EINVAL;
91 }
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010092 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070093
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080094 newchannel->onchannel_callback = onchannelcallback;
95 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070096
Bill Pemberton454f18a2009-07-27 16:47:24 -040097 /* Allocate the ring buffer */
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070098 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
99 GFP_KERNEL|__GFP_ZERO,
100 get_order(send_ringbuffer_size +
101 recv_ringbuffer_size));
102
103 if (!page)
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700104 page = alloc_pages(GFP_KERNEL|__GFP_ZERO,
105 get_order(send_ringbuffer_size +
106 recv_ringbuffer_size));
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800107
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700108 if (!page) {
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800109 err = -ENOMEM;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700110 goto error_set_chnstate;
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800111 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700112
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700113 newchannel->ringbuffer_pages = page_address(page);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800114 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700115 recv_ringbuffer_size) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700116
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700117 ret = hv_ringbuffer_init(&newchannel->outbound, page,
118 send_ringbuffer_size >> PAGE_SHIFT);
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700119
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000120 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400121 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700122 goto error_free_pages;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400123 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700124
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700125 ret = hv_ringbuffer_init(&newchannel->inbound,
126 &page[send_ringbuffer_size >> PAGE_SHIFT],
127 recv_ringbuffer_size >> PAGE_SHIFT);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000128 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400129 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700130 goto error_free_pages;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400131 }
132
Hank Janssen3e7ee492009-07-13 16:02:34 -0700133
Bill Pemberton454f18a2009-07-27 16:47:24 -0400134 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800135 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700136
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700137 ret = vmbus_establish_gpadl(newchannel,
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700138 page_address(page),
139 send_ringbuffer_size +
140 recv_ringbuffer_size,
141 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400142
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000143 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400144 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700145 goto error_free_pages;
Bill Pembertonb94ef342010-05-05 15:27:37 -0400146 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700147
Bill Pemberton454f18a2009-07-27 16:47:24 -0400148 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700149 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700150 sizeof(struct vmbus_channel_open_channel),
151 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700152 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400153 err = -ENOMEM;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700154 goto error_free_gpadl;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400155 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700156
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700157 init_completion(&open_info->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800158 open_info->waiting_channel = newchannel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700159
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700160 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700161 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
162 open_msg->openid = newchannel->offermsg.child_relid;
163 open_msg->child_relid = newchannel->offermsg.child_relid;
164 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
165 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700166 PAGE_SHIFT;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800167 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700168
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700169 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400170 err = -EINVAL;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700171 goto error_free_gpadl;
Bill Pembertonc827f942010-05-05 15:27:38 -0400172 }
173
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700174 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700175 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700176
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800177 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700178 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800179 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800180 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700181
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -0700182 if (newchannel->rescind) {
183 err = -ENODEV;
184 goto error_free_gpadl;
185 }
186
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700187 ret = vmbus_post_msg(open_msg,
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800188 sizeof(struct vmbus_channel_open_channel), true);
Hank Janssen98e08702011-03-29 13:58:44 -0700189
Vitaly Kuznetsov9202d772017-10-29 12:21:09 -0700190 trace_vmbus_open(open_msg, ret);
191
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700192 if (ret != 0) {
193 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700194 goto error_clean_msglist;
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700195 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700196
Vitaly Kuznetsov396e2872016-06-09 17:08:56 -0700197 wait_for_completion(&open_info->waitevent);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800198
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800199 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700200 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800201 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700202
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800203 if (newchannel->rescind) {
204 err = -ENODEV;
205 goto error_free_gpadl;
206 }
207
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700208 if (open_info->response.open_result.status) {
209 err = -EAGAIN;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700210 goto error_free_gpadl;
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700211 }
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700212
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700213 newchannel->state = CHANNEL_OPENED_STATE;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700214 kfree(open_info);
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700215 return 0;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400216
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700217error_clean_msglist:
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700218 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
219 list_del(&open_info->msglistentry);
220 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
221
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700222error_free_gpadl:
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800223 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700224 kfree(open_info);
225error_free_pages:
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700226 hv_ringbuffer_cleanup(&newchannel->outbound);
227 hv_ringbuffer_cleanup(&newchannel->inbound);
228 __free_pages(page,
229 get_order(send_ringbuffer_size + recv_ringbuffer_size));
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700230error_set_chnstate:
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800231 newchannel->state = CHANNEL_OPEN_STATE;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400232 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700233}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700234EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700235
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800236/* Used for Hyper-V Socket: a guest client's connect() to the host */
237int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
238 const uuid_le *shv_host_servie_id)
239{
240 struct vmbus_channel_tl_connect_request conn_msg;
Vitaly Kuznetsov98f31a02017-10-29 12:21:15 -0700241 int ret;
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800242
243 memset(&conn_msg, 0, sizeof(conn_msg));
244 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
245 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
246 conn_msg.host_service_id = *shv_host_servie_id;
247
Vitaly Kuznetsov98f31a02017-10-29 12:21:15 -0700248 ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
249
250 trace_vmbus_send_tl_connect_request(&conn_msg, ret);
251
252 return ret;
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800253}
254EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
255
Hank Janssen3e189512010-03-04 22:11:00 +0000256/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700257 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700258 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700259static int create_gpadl_header(void *kbuffer, u32 size,
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700260 struct vmbus_channel_msginfo **msginfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700261{
262 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700263 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700264 struct vmbus_channel_gpadl_header *gpadl_header;
265 struct vmbus_channel_gpadl_body *gpadl_body;
266 struct vmbus_channel_msginfo *msgheader;
267 struct vmbus_channel_msginfo *msgbody = NULL;
268 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700269
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700270 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700271
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700272 pagecount = size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700273
Bill Pemberton454f18a2009-07-27 16:47:24 -0400274 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700275 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700276 sizeof(struct vmbus_channel_gpadl_header) -
277 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700278 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700279
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700280 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700281 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400282 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700283 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700284 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700285 sizeof(struct gpa_range) + pfncount * sizeof(u64);
286 msgheader = kzalloc(msgsize, GFP_KERNEL);
287 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400288 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700289
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800290 INIT_LIST_HEAD(&msgheader->submsglist);
291 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700292
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700293 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800294 msgheader->msg;
295 gpadl_header->rangecount = 1;
296 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700297 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800298 gpadl_header->range[0].byte_offset = 0;
299 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700300 for (i = 0; i < pfncount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800301 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
302 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700303 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700304
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700305 pfnsum = pfncount;
306 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700307
Bill Pemberton454f18a2009-07-27 16:47:24 -0400308 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700309 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700310 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700311 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700312
Bill Pemberton454f18a2009-07-27 16:47:24 -0400313 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700314 while (pfnleft) {
315 if (pfnleft > pfncount)
316 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700317 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700318 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700319
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700320 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700321 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700322 pfncurr * sizeof(u64);
323 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700324
325 if (!msgbody) {
326 struct vmbus_channel_msginfo *pos = NULL;
327 struct vmbus_channel_msginfo *tmp = NULL;
328 /*
329 * Free up all the allocated messages.
330 */
331 list_for_each_entry_safe(pos, tmp,
332 &msgheader->submsglist,
333 msglistentry) {
334
335 list_del(&pos->msglistentry);
336 kfree(pos);
337 }
338
Bill Pembertond1c250b2010-05-05 15:27:35 -0400339 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700340 }
341
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800342 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700343 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800344 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700345
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700346 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700347 * Gpadl is u32 and we are using a pointer which could
348 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700349 * This is governed by the guest/host protocol and
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700350 * so the hypervisor guarantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700351 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700352 for (i = 0; i < pfncurr; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800353 gpadl_body->pfn[i] = slow_virt_to_phys(
354 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
355 PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700356
Bill Pemberton454f18a2009-07-27 16:47:24 -0400357 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800358 list_add_tail(&msgbody->msglistentry,
359 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700360 pfnsum += pfncurr;
361 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700362 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700363 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400364 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700365 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700366 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700367 sizeof(struct gpa_range) + pagecount * sizeof(u64);
368 msgheader = kzalloc(msgsize, GFP_KERNEL);
369 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400370 goto nomem;
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700371
372 INIT_LIST_HEAD(&msgheader->submsglist);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800373 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700374
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700375 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800376 msgheader->msg;
377 gpadl_header->rangecount = 1;
378 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700379 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800380 gpadl_header->range[0].byte_offset = 0;
381 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700382 for (i = 0; i < pagecount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800383 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
384 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700385
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700386 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700387 }
388
389 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400390nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700391 kfree(msgheader);
392 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400393 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700394}
395
Hank Janssen3e189512010-03-04 22:11:00 +0000396/*
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700397 * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700398 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700399 * @channel: a channel
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800400 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700401 * @size: page-size multiple
402 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700403 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700404int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700405 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700406{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700407 struct vmbus_channel_gpadl_header *gpadlmsg;
408 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700409 struct vmbus_channel_msginfo *msginfo = NULL;
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700410 struct vmbus_channel_msginfo *submsginfo, *tmp;
Bill Pemberton53af5452009-09-11 21:46:44 -0400411 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700412 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700413 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400414 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700415
K. Y. Srinivasan9f52a162015-01-09 23:54:33 -0800416 next_gpadl_handle =
417 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700418
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700419 ret = create_gpadl_header(kbuffer, size, &msginfo);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400420 if (ret)
421 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700422
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700423 init_completion(&msginfo->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800424 msginfo->waiting_channel = channel;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400425
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800426 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
427 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
428 gpadlmsg->child_relid = channel->offermsg.child_relid;
429 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700430
Hank Janssen3e7ee492009-07-13 16:02:34 -0700431
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800432 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800433 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800434 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700435
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800436 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700437
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -0700438 if (channel->rescind) {
439 ret = -ENODEV;
440 goto cleanup;
441 }
442
Haiyang Zhangc6977672011-01-26 12:12:08 -0800443 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800444 sizeof(*msginfo), true);
Vitaly Kuznetsov69edbd52017-10-29 12:21:11 -0700445
446 trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
447
Hank Janssen98e08702011-03-29 13:58:44 -0700448 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700449 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700450
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700451 list_for_each(curr, &msginfo->submsglist) {
452 submsginfo = (struct vmbus_channel_msginfo *)curr;
453 gpadl_body =
454 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Bill Pemberton53af5452009-09-11 21:46:44 -0400455
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700456 gpadl_body->header.msgtype =
457 CHANNELMSG_GPADL_BODY;
458 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700459
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700460 ret = vmbus_post_msg(gpadl_body,
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800461 submsginfo->msgsize - sizeof(*submsginfo),
462 true);
Vitaly Kuznetsov69edbd52017-10-29 12:21:11 -0700463
464 trace_vmbus_establish_gpadl_body(gpadl_body, ret);
465
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700466 if (ret != 0)
467 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700468
Hank Janssen3e7ee492009-07-13 16:02:34 -0700469 }
K. Y. Srinivasan72c6b712014-08-27 16:25:34 -0700470 wait_for_completion(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700471
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800472 if (channel->rescind) {
473 ret = -ENODEV;
474 goto cleanup;
475 }
476
Bill Pemberton454f18a2009-07-27 16:47:24 -0400477 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800478 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700479
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700480cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800481 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800482 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800483 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700484 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
485 msglistentry) {
486 kfree(submsginfo);
487 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700488
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700489 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700490 return ret;
491}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700492EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700493
Hank Janssen3e189512010-03-04 22:11:00 +0000494/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700495 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700496 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700497int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700498{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700499 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700500 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700501 unsigned long flags;
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700502 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700503
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700504 info = kmalloc(sizeof(*info) +
505 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400506 if (!info)
507 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700508
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700509 init_completion(&info->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800510 info->waiting_channel = channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700511
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800512 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700513
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800514 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
515 msg->child_relid = channel->offermsg.child_relid;
516 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700517
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800518 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800519 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800520 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800521 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -0700522
523 if (channel->rescind)
524 goto post_msg_err;
525
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800526 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
527 true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700528
Vitaly Kuznetsov09cdf8f2017-10-29 12:21:12 -0700529 trace_vmbus_teardown_gpadl(msg, ret);
530
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700531 if (ret)
532 goto post_msg_err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700533
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700534 wait_for_completion(&info->waitevent);
535
536post_msg_err:
K. Y. Srinivasan5e030d52017-03-12 20:00:30 -0700537 /*
538 * If the channel has been rescinded;
539 * we will be awakened by the rescind
540 * handler; set the error code to zero so we don't leak memory.
541 */
542 if (channel->rescind)
543 ret = 0;
544
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800545 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800546 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800547 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700548
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700549 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700550 return ret;
551}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700552EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700553
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700554static void reset_channel_cb(void *arg)
555{
556 struct vmbus_channel *channel = arg;
557
558 channel->onchannel_callback = NULL;
559}
560
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700561static int vmbus_close_internal(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700562{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700563 struct vmbus_channel_close_channel *msg;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700564 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700565
Dexuan Cui63d55b22015-12-14 16:01:47 -0800566 /*
Dexuan Cuidad72a12017-03-04 18:13:58 -0700567 * vmbus_on_event(), running in the per-channel tasklet, can race
Dexuan Cui63d55b22015-12-14 16:01:47 -0800568 * with vmbus_close_internal() in the case of SMP guest, e.g., when
569 * the former is accessing channel->inbound.ring_buffer, the latter
Dexuan Cuidad72a12017-03-04 18:13:58 -0700570 * could be freeing the ring_buffer pages, so here we must stop it
571 * first.
Dexuan Cui63d55b22015-12-14 16:01:47 -0800572 */
Dexuan Cuidad72a12017-03-04 18:13:58 -0700573 tasklet_disable(&channel->callback_event);
Dexuan Cui63d55b22015-12-14 16:01:47 -0800574
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800575 /*
576 * In case a device driver's probe() fails (e.g.,
577 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
Masahiro Yamada8a1115f2017-03-09 16:16:31 -0800578 * rescinded later (e.g., we dynamically disable an Integrated Service
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800579 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
580 * here we should skip most of the below cleanup work.
581 */
582 if (channel->state != CHANNEL_OPENED_STATE) {
583 ret = -EINVAL;
584 goto out;
585 }
586
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700587 channel->state = CHANNEL_OPEN_STATE;
588 channel->sc_creation_callback = NULL;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400589 /* Stop callback and cancel the timer asap */
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700590 if (channel->target_cpu != get_cpu()) {
591 put_cpu();
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700592 smp_call_function_single(channel->target_cpu, reset_channel_cb,
593 channel, true);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700594 } else {
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700595 reset_channel_cb(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700596 put_cpu();
597 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700598
Bill Pemberton454f18a2009-07-27 16:47:24 -0400599 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700600
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700601 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700602
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800603 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
604 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700605
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800606 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
607 true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700608
Vitaly Kuznetsov633b0052017-10-29 12:21:10 -0700609 trace_vmbus_close_internal(msg, ret);
610
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700611 if (ret) {
612 pr_err("Close failed: close post msg return is %d\n", ret);
613 /*
614 * If we failed to post the close msg,
615 * it is perhaps better to leak memory.
616 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800617 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700618 }
619
Bill Pemberton454f18a2009-07-27 16:47:24 -0400620 /* Tear down the gpadl for the channel's ring buffer */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700621 if (channel->ringbuffer_gpadlhandle) {
622 ret = vmbus_teardown_gpadl(channel,
623 channel->ringbuffer_gpadlhandle);
624 if (ret) {
625 pr_err("Close failed: teardown gpadl return %d\n", ret);
626 /*
627 * If we failed to teardown gpadl,
628 * it is perhaps better to leak memory.
629 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800630 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700631 }
632 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700633
Bill Pemberton454f18a2009-07-27 16:47:24 -0400634 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700635 hv_ringbuffer_cleanup(&channel->outbound);
636 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700637
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800638 free_pages((unsigned long)channel->ringbuffer_pages,
639 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700640
Dexuan Cui63d55b22015-12-14 16:01:47 -0800641out:
Stephen Hemminger6463a452017-06-25 12:47:46 -0700642 /* re-enable tasklet for use on re-open */
643 tasklet_enable(&channel->callback_event);
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700644 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700645}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700646
647/*
648 * vmbus_close - Close the specified channel
649 */
650void vmbus_close(struct vmbus_channel *channel)
651{
652 struct list_head *cur, *tmp;
653 struct vmbus_channel *cur_channel;
654
655 if (channel->primary_channel != NULL) {
656 /*
657 * We will only close sub-channels when
658 * the primary is closed.
659 */
660 return;
661 }
662 /*
663 * Close all the sub-channels first and then close the
664 * primary channel.
665 */
666 list_for_each_safe(cur, tmp, &channel->sc_list) {
667 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
K. Y. Srinivasan54a66262017-04-30 16:21:18 -0700668 if (cur_channel->rescind) {
K. Y. Srinivasan7fa32e52017-11-14 06:53:33 -0700669 wait_for_completion(&cur_channel->rescind_event);
670 mutex_lock(&vmbus_connection.channel_mutex);
671 vmbus_close_internal(cur_channel);
K. Y. Srinivasan192b2d72017-09-29 21:09:36 -0700672 hv_process_channel_removal(
K. Y. Srinivasan54a66262017-04-30 16:21:18 -0700673 cur_channel->offermsg.child_relid);
K. Y. Srinivasan7fa32e52017-11-14 06:53:33 -0700674 } else {
675 mutex_lock(&vmbus_connection.channel_mutex);
676 vmbus_close_internal(cur_channel);
K. Y. Srinivasan54a66262017-04-30 16:21:18 -0700677 }
K. Y. Srinivasan7fa32e52017-11-14 06:53:33 -0700678 mutex_unlock(&vmbus_connection.channel_mutex);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700679 }
680 /*
681 * Now close the primary.
682 */
K. Y. Srinivasan7fa32e52017-11-14 06:53:33 -0700683 mutex_lock(&vmbus_connection.channel_mutex);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700684 vmbus_close_internal(channel);
K. Y. Srinivasan192b2d72017-09-29 21:09:36 -0700685 mutex_unlock(&vmbus_connection.channel_mutex);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700686}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700687EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700688
stephen hemminger5dd0fb92017-08-16 08:56:26 -0700689/**
690 * vmbus_sendpacket() - Send the specified buffer on the given channel
691 * @channel: Pointer to vmbus_channel structure.
692 * @buffer: Pointer to the buffer you want to receive the data into.
693 * @bufferlen: Maximum size of what the the buffer will hold
694 * @requestid: Identifier of the request
695 * @type: Type of packet that is being send e.g. negotiate, time
696 * packet etc.
697 *
698 * Sends data in @buffer directly to hyper-v via the vmbus
699 * This will send the data unparsed to hyper-v.
700 *
701 * Mainly used by Hyper-V drivers.
702 */
703int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
704 u32 bufferlen, u64 requestid,
705 enum vmbus_packet_type type, u32 flags)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700706{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700707 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700708 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100709 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800710 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700711 u64 aligned_data = 0;
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700712 int num_vecs = ((bufferlen != 0) ? 3 : 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700713
Hank Janssen3e7ee492009-07-13 16:02:34 -0700714
Bill Pemberton454f18a2009-07-27 16:47:24 -0400715 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800716 desc.type = type; /* VmbusPacketTypeDataInBand; */
717 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700718 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800719 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
720 desc.len8 = (u16)(packetlen_aligned >> 3);
721 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700722
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800723 bufferlist[0].iov_base = &desc;
724 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
725 bufferlist[1].iov_base = buffer;
726 bufferlist[1].iov_len = bufferlen;
727 bufferlist[2].iov_base = &aligned_data;
728 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700729
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700730 return hv_ringbuffer_write(channel, bufferlist, num_vecs);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700731}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700732EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700733
Hank Janssen3e189512010-03-04 22:11:00 +0000734/*
stephen hemminger5a668d82017-08-16 08:56:25 -0700735 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800736 * packets using a GPADL Direct packet type. This interface allows you
737 * to control notifying the host. This will be useful for sending
738 * batched data. Also the sender can control the send flags
739 * explicitly.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700740 */
stephen hemminger5a668d82017-08-16 08:56:25 -0700741int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
742 struct hv_page_buffer pagebuffers[],
743 u32 pagecount, void *buffer, u32 bufferlen,
744 u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700745{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700746 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000747 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700748 u32 descsize;
749 u32 packetlen;
750 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800751 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700752 u64 aligned_data = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700753
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700754 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400755 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700756
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700757 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000758 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700759 * largest size we support
760 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700761 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
762 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700763 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700764 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100765 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700766
Bill Pemberton454f18a2009-07-27 16:47:24 -0400767 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800768 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
stephen hemminger5a668d82017-08-16 08:56:25 -0700769 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700770 desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700771 desc.length8 = (u16)(packetlen_aligned >> 3);
772 desc.transactionid = requestid;
Stephen Hemminger33d426a2017-10-05 17:35:05 -0700773 desc.reserved = 0;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700774 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700775
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700776 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800777 desc.range[i].len = pagebuffers[i].len;
778 desc.range[i].offset = pagebuffers[i].offset;
779 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700780 }
781
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800782 bufferlist[0].iov_base = &desc;
783 bufferlist[0].iov_len = descsize;
784 bufferlist[1].iov_base = buffer;
785 bufferlist[1].iov_len = bufferlen;
786 bufferlist[2].iov_base = &aligned_data;
787 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700788
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700789 return hv_ringbuffer_write(channel, bufferlist, 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700790}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700791EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700792
Hank Janssen3e189512010-03-04 22:11:00 +0000793/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700794 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000795 * using a GPADL Direct packet type.
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800796 * The buffer includes the vmbus descriptor.
797 */
798int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
799 struct vmbus_packet_mpb_array *desc,
800 u32 desc_size,
801 void *buffer, u32 bufferlen, u64 requestid)
802{
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800803 u32 packetlen;
804 u32 packetlen_aligned;
805 struct kvec bufferlist[3];
806 u64 aligned_data = 0;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800807
808 packetlen = desc_size + bufferlen;
809 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
810
811 /* Setup the descriptor */
812 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
813 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Stephen Hemmingerbdc1dd42017-03-04 18:27:14 -0700814 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800815 desc->length8 = (u16)(packetlen_aligned >> 3);
816 desc->transactionid = requestid;
Stephen Hemminger33d426a2017-10-05 17:35:05 -0700817 desc->reserved = 0;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800818 desc->rangecount = 1;
819
820 bufferlist[0].iov_base = desc;
821 bufferlist[0].iov_len = desc_size;
822 bufferlist[1].iov_base = buffer;
823 bufferlist[1].iov_len = bufferlen;
824 bufferlist[2].iov_base = &aligned_data;
825 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
826
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700827 return hv_ringbuffer_write(channel, bufferlist, 3);
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800828}
829EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
830
Hank Janssenc88c4e42010-05-04 15:55:05 -0700831/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700832 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700833 * @channel: Pointer to vmbus_channel structure.
834 * @buffer: Pointer to the buffer you want to receive the data into.
835 * @bufferlen: Maximum size of what the the buffer will hold
836 * @buffer_actual_len: The actual size of the data after it was received
837 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700838 *
839 * Receives directly from the hyper-v vmbus and puts the data it received
840 * into Buffer. This will receive the data unparsed from hyper-v.
841 *
842 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700843 */
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800844static inline int
845__vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
846 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
847 bool raw)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700848{
K. Y. Srinivasan33725922016-11-06 13:14:18 -0800849 return hv_ringbuffer_read(channel, buffer, bufferlen,
850 buffer_actual_len, requestid, raw);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700851
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800852}
853
854int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
855 u32 bufferlen, u32 *buffer_actual_len,
856 u64 *requestid)
857{
858 return __vmbus_recvpacket(channel, buffer, bufferlen,
859 buffer_actual_len, requestid, false);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700860}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700861EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700862
Hank Janssen3e189512010-03-04 22:11:00 +0000863/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700864 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700865 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700866int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700867 u32 bufferlen, u32 *buffer_actual_len,
868 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700869{
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800870 return __vmbus_recvpacket(channel, buffer, bufferlen,
871 buffer_actual_len, requestid, true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700872}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700873EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);