blob: 81a80c82f1bd2b6a55df393a3df55376d709adfd [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
K. Y. Srinivasan37242872016-07-01 16:26:37 -070046 /*
47 * For channels marked as in "low latency" mode
48 * bypass the monitor page mechanism.
49 */
Stephen Hemminger5c1bec62017-02-05 17:20:31 -070050 if (channel->offermsg.monitor_allocated && !channel->low_latency) {
51 vmbus_send_interrupt(channel->offermsg.child_relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -070052
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -070053 /* Get the child to parent monitor page */
54 monitorpage = vmbus_connection.monitor_pages[1];
Hank Janssen3e7ee492009-07-13 16:02:34 -070055
Olaf Hering22356582011-03-21 14:41:37 +010056 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080057 (unsigned long *)&monitorpage->trigger_group
58 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -040059
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070060 } else {
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -080061 vmbus_set_event(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -070062 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070063}
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080064EXPORT_SYMBOL_GPL(vmbus_setevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -070065
Hank Janssen3e189512010-03-04 22:11:00 +000066/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070067 * vmbus_open - Open the specified channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070068 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070069int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -070070 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
71 void (*onchannelcallback)(void *context), void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -070072{
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -070073 struct vmbus_channel_open_channel *open_msg;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -070074 struct vmbus_channel_msginfo *open_info = NULL;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070075 unsigned long flags;
Nicholas Mc Guire08a95132015-02-27 11:26:01 -080076 int ret, err = 0;
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070077 struct page *page;
Hank Janssen3e7ee492009-07-13 16:02:34 -070078
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -070079 if (send_ringbuffer_size % PAGE_SIZE ||
80 recv_ringbuffer_size % PAGE_SIZE)
81 return -EINVAL;
82
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010083 spin_lock_irqsave(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070084 if (newchannel->state == CHANNEL_OPEN_STATE) {
85 newchannel->state = CHANNEL_OPENING_STATE;
86 } else {
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010087 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070088 return -EINVAL;
89 }
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010090 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070091
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080092 newchannel->onchannel_callback = onchannelcallback;
93 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070094
Bill Pemberton454f18a2009-07-27 16:47:24 -040095 /* Allocate the ring buffer */
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070096 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
97 GFP_KERNEL|__GFP_ZERO,
98 get_order(send_ringbuffer_size +
99 recv_ringbuffer_size));
100
101 if (!page)
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700102 page = alloc_pages(GFP_KERNEL|__GFP_ZERO,
103 get_order(send_ringbuffer_size +
104 recv_ringbuffer_size));
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800105
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700106 if (!page) {
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800107 err = -ENOMEM;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700108 goto error_set_chnstate;
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800109 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700110
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700111 newchannel->ringbuffer_pages = page_address(page);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800112 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700113 recv_ringbuffer_size) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700114
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700115 ret = hv_ringbuffer_init(&newchannel->outbound, page,
116 send_ringbuffer_size >> PAGE_SHIFT);
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700117
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000118 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400119 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700120 goto error_free_pages;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400121 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700122
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700123 ret = hv_ringbuffer_init(&newchannel->inbound,
124 &page[send_ringbuffer_size >> PAGE_SHIFT],
125 recv_ringbuffer_size >> PAGE_SHIFT);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000126 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400127 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700128 goto error_free_pages;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400129 }
130
Hank Janssen3e7ee492009-07-13 16:02:34 -0700131
Bill Pemberton454f18a2009-07-27 16:47:24 -0400132 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800133 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700134
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700135 ret = vmbus_establish_gpadl(newchannel,
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700136 page_address(page),
137 send_ringbuffer_size +
138 recv_ringbuffer_size,
139 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400140
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000141 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400142 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700143 goto error_free_pages;
Bill Pembertonb94ef342010-05-05 15:27:37 -0400144 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700145
Bill Pemberton454f18a2009-07-27 16:47:24 -0400146 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700147 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700148 sizeof(struct vmbus_channel_open_channel),
149 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700150 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400151 err = -ENOMEM;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700152 goto error_free_gpadl;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400153 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700154
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700155 init_completion(&open_info->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800156 open_info->waiting_channel = newchannel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700157
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700158 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700159 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
160 open_msg->openid = newchannel->offermsg.child_relid;
161 open_msg->child_relid = newchannel->offermsg.child_relid;
162 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
163 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700164 PAGE_SHIFT;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800165 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700166
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700167 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400168 err = -EINVAL;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700169 goto error_free_gpadl;
Bill Pembertonc827f942010-05-05 15:27:38 -0400170 }
171
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700172 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700173 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700174
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800175 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700176 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800177 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800178 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700179
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700180 ret = vmbus_post_msg(open_msg,
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800181 sizeof(struct vmbus_channel_open_channel), true);
Hank Janssen98e08702011-03-29 13:58:44 -0700182
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700183 if (ret != 0) {
184 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700185 goto error_clean_msglist;
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700186 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700187
Vitaly Kuznetsov396e2872016-06-09 17:08:56 -0700188 wait_for_completion(&open_info->waitevent);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800189
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800190 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700191 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800192 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700193
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800194 if (newchannel->rescind) {
195 err = -ENODEV;
196 goto error_free_gpadl;
197 }
198
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700199 if (open_info->response.open_result.status) {
200 err = -EAGAIN;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700201 goto error_free_gpadl;
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700202 }
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700203
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700204 newchannel->state = CHANNEL_OPENED_STATE;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700205 kfree(open_info);
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700206 return 0;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400207
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700208error_clean_msglist:
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700209 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
210 list_del(&open_info->msglistentry);
211 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
212
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700213error_free_gpadl:
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800214 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700215 kfree(open_info);
216error_free_pages:
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700217 hv_ringbuffer_cleanup(&newchannel->outbound);
218 hv_ringbuffer_cleanup(&newchannel->inbound);
219 __free_pages(page,
220 get_order(send_ringbuffer_size + recv_ringbuffer_size));
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700221error_set_chnstate:
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800222 newchannel->state = CHANNEL_OPEN_STATE;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400223 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700224}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700225EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700226
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800227/* Used for Hyper-V Socket: a guest client's connect() to the host */
228int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
229 const uuid_le *shv_host_servie_id)
230{
231 struct vmbus_channel_tl_connect_request conn_msg;
232
233 memset(&conn_msg, 0, sizeof(conn_msg));
234 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
235 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
236 conn_msg.host_service_id = *shv_host_servie_id;
237
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800238 return vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800239}
240EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
241
Hank Janssen3e189512010-03-04 22:11:00 +0000242/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700243 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700244 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700245static int create_gpadl_header(void *kbuffer, u32 size,
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700246 struct vmbus_channel_msginfo **msginfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700247{
248 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700249 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700250 struct vmbus_channel_gpadl_header *gpadl_header;
251 struct vmbus_channel_gpadl_body *gpadl_body;
252 struct vmbus_channel_msginfo *msgheader;
253 struct vmbus_channel_msginfo *msgbody = NULL;
254 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700255
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700256 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700257
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700258 pagecount = size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700259
Bill Pemberton454f18a2009-07-27 16:47:24 -0400260 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700261 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700262 sizeof(struct vmbus_channel_gpadl_header) -
263 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700264 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700265
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700266 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700267 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400268 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700269 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700270 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700271 sizeof(struct gpa_range) + pfncount * sizeof(u64);
272 msgheader = kzalloc(msgsize, GFP_KERNEL);
273 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400274 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700275
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800276 INIT_LIST_HEAD(&msgheader->submsglist);
277 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700278
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700279 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800280 msgheader->msg;
281 gpadl_header->rangecount = 1;
282 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700283 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800284 gpadl_header->range[0].byte_offset = 0;
285 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700286 for (i = 0; i < pfncount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800287 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
288 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700289 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700290
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700291 pfnsum = pfncount;
292 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700293
Bill Pemberton454f18a2009-07-27 16:47:24 -0400294 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700295 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700296 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700297 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700298
Bill Pemberton454f18a2009-07-27 16:47:24 -0400299 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700300 while (pfnleft) {
301 if (pfnleft > pfncount)
302 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700303 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700304 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700305
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700306 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700307 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700308 pfncurr * sizeof(u64);
309 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700310
311 if (!msgbody) {
312 struct vmbus_channel_msginfo *pos = NULL;
313 struct vmbus_channel_msginfo *tmp = NULL;
314 /*
315 * Free up all the allocated messages.
316 */
317 list_for_each_entry_safe(pos, tmp,
318 &msgheader->submsglist,
319 msglistentry) {
320
321 list_del(&pos->msglistentry);
322 kfree(pos);
323 }
324
Bill Pembertond1c250b2010-05-05 15:27:35 -0400325 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700326 }
327
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800328 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700329 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800330 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700331
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700332 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700333 * Gpadl is u32 and we are using a pointer which could
334 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700335 * This is governed by the guest/host protocol and
336 * so the hypervisor gurantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700337 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700338 for (i = 0; i < pfncurr; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800339 gpadl_body->pfn[i] = slow_virt_to_phys(
340 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
341 PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700342
Bill Pemberton454f18a2009-07-27 16:47:24 -0400343 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800344 list_add_tail(&msgbody->msglistentry,
345 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700346 pfnsum += pfncurr;
347 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700348 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700349 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400350 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700351 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700352 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700353 sizeof(struct gpa_range) + pagecount * sizeof(u64);
354 msgheader = kzalloc(msgsize, GFP_KERNEL);
355 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400356 goto nomem;
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700357
358 INIT_LIST_HEAD(&msgheader->submsglist);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800359 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700360
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700361 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800362 msgheader->msg;
363 gpadl_header->rangecount = 1;
364 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700365 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800366 gpadl_header->range[0].byte_offset = 0;
367 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700368 for (i = 0; i < pagecount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800369 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
370 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700371
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700372 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700373 }
374
375 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400376nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700377 kfree(msgheader);
378 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400379 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700380}
381
Hank Janssen3e189512010-03-04 22:11:00 +0000382/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700383 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700384 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700385 * @channel: a channel
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800386 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700387 * @size: page-size multiple
388 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700389 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700390int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700391 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700392{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700393 struct vmbus_channel_gpadl_header *gpadlmsg;
394 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700395 struct vmbus_channel_msginfo *msginfo = NULL;
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700396 struct vmbus_channel_msginfo *submsginfo, *tmp;
Bill Pemberton53af5452009-09-11 21:46:44 -0400397 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700398 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700399 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400400 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700401
K. Y. Srinivasan9f52a162015-01-09 23:54:33 -0800402 next_gpadl_handle =
403 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700404
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700405 ret = create_gpadl_header(kbuffer, size, &msginfo);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400406 if (ret)
407 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700408
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700409 init_completion(&msginfo->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800410 msginfo->waiting_channel = channel;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400411
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800412 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
413 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
414 gpadlmsg->child_relid = channel->offermsg.child_relid;
415 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700416
Hank Janssen3e7ee492009-07-13 16:02:34 -0700417
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800418 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800419 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800420 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700421
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800422 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700423
Haiyang Zhangc6977672011-01-26 12:12:08 -0800424 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800425 sizeof(*msginfo), true);
Hank Janssen98e08702011-03-29 13:58:44 -0700426 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700427 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700428
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700429 list_for_each(curr, &msginfo->submsglist) {
430 submsginfo = (struct vmbus_channel_msginfo *)curr;
431 gpadl_body =
432 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Bill Pemberton53af5452009-09-11 21:46:44 -0400433
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700434 gpadl_body->header.msgtype =
435 CHANNELMSG_GPADL_BODY;
436 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700437
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700438 ret = vmbus_post_msg(gpadl_body,
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800439 submsginfo->msgsize - sizeof(*submsginfo),
440 true);
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700441 if (ret != 0)
442 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700443
Hank Janssen3e7ee492009-07-13 16:02:34 -0700444 }
K. Y. Srinivasan72c6b712014-08-27 16:25:34 -0700445 wait_for_completion(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700446
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800447 if (channel->rescind) {
448 ret = -ENODEV;
449 goto cleanup;
450 }
451
Bill Pemberton454f18a2009-07-27 16:47:24 -0400452 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800453 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700454
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700455cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800456 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800457 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800458 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700459 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
460 msglistentry) {
461 kfree(submsginfo);
462 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700463
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700464 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700465 return ret;
466}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700467EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700468
Hank Janssen3e189512010-03-04 22:11:00 +0000469/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700470 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700471 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700472int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700473{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700474 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700475 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700476 unsigned long flags;
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700477 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700478
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700479 info = kmalloc(sizeof(*info) +
480 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400481 if (!info)
482 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700483
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700484 init_completion(&info->waitevent);
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800485 info->waiting_channel = channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700486
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800487 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700488
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800489 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
490 msg->child_relid = channel->offermsg.child_relid;
491 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700492
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800493 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800494 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800495 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800496 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800497 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
498 true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700499
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700500 if (ret)
501 goto post_msg_err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700502
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700503 wait_for_completion(&info->waitevent);
504
K. Y. Srinivasanccb61f82016-12-22 16:54:00 -0800505 if (channel->rescind) {
506 ret = -ENODEV;
507 goto post_msg_err;
508 }
509
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700510post_msg_err:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800511 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800512 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800513 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700514
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700515 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700516 return ret;
517}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700518EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700519
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700520static void reset_channel_cb(void *arg)
521{
522 struct vmbus_channel *channel = arg;
523
524 channel->onchannel_callback = NULL;
525}
526
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700527static int vmbus_close_internal(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700528{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700529 struct vmbus_channel_close_channel *msg;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700530 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700531
Dexuan Cui63d55b22015-12-14 16:01:47 -0800532 /*
Stephen Hemminger631e63a2017-02-11 23:02:20 -0700533 * vmbus_on_event(), running in the tasklet, can race
Dexuan Cui63d55b22015-12-14 16:01:47 -0800534 * with vmbus_close_internal() in the case of SMP guest, e.g., when
535 * the former is accessing channel->inbound.ring_buffer, the latter
536 * could be freeing the ring_buffer pages.
537 *
538 * To resolve the race, we can serialize them by disabling the
539 * tasklet when the latter is running here.
540 */
Dexuan Cui638fea32016-06-09 18:47:24 -0700541 hv_event_tasklet_disable(channel);
Dexuan Cui63d55b22015-12-14 16:01:47 -0800542
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800543 /*
544 * In case a device driver's probe() fails (e.g.,
545 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
546 * rescinded later (e.g., we dynamically disble an Integrated Service
547 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
548 * here we should skip most of the below cleanup work.
549 */
550 if (channel->state != CHANNEL_OPENED_STATE) {
551 ret = -EINVAL;
552 goto out;
553 }
554
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700555 channel->state = CHANNEL_OPEN_STATE;
556 channel->sc_creation_callback = NULL;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400557 /* Stop callback and cancel the timer asap */
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700558 if (channel->target_cpu != get_cpu()) {
559 put_cpu();
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700560 smp_call_function_single(channel->target_cpu, reset_channel_cb,
561 channel, true);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700562 } else {
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700563 reset_channel_cb(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700564 put_cpu();
565 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700566
Bill Pemberton454f18a2009-07-27 16:47:24 -0400567 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700568
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700569 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700570
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800571 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
572 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700573
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800574 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
575 true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700576
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700577 if (ret) {
578 pr_err("Close failed: close post msg return is %d\n", ret);
579 /*
580 * If we failed to post the close msg,
581 * it is perhaps better to leak memory.
582 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800583 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700584 }
585
Bill Pemberton454f18a2009-07-27 16:47:24 -0400586 /* Tear down the gpadl for the channel's ring buffer */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700587 if (channel->ringbuffer_gpadlhandle) {
588 ret = vmbus_teardown_gpadl(channel,
589 channel->ringbuffer_gpadlhandle);
590 if (ret) {
591 pr_err("Close failed: teardown gpadl return %d\n", ret);
592 /*
593 * If we failed to teardown gpadl,
594 * it is perhaps better to leak memory.
595 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800596 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700597 }
598 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700599
Bill Pemberton454f18a2009-07-27 16:47:24 -0400600 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700601 hv_ringbuffer_cleanup(&channel->outbound);
602 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700603
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800604 free_pages((unsigned long)channel->ringbuffer_pages,
605 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700606
Dexuan Cui63d55b22015-12-14 16:01:47 -0800607out:
Dexuan Cui638fea32016-06-09 18:47:24 -0700608 hv_event_tasklet_enable(channel);
Dexuan Cui63d55b22015-12-14 16:01:47 -0800609
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700610 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700611}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700612
613/*
614 * vmbus_close - Close the specified channel
615 */
616void vmbus_close(struct vmbus_channel *channel)
617{
618 struct list_head *cur, *tmp;
619 struct vmbus_channel *cur_channel;
620
621 if (channel->primary_channel != NULL) {
622 /*
623 * We will only close sub-channels when
624 * the primary is closed.
625 */
626 return;
627 }
628 /*
629 * Close all the sub-channels first and then close the
630 * primary channel.
631 */
632 list_for_each_safe(cur, tmp, &channel->sc_list) {
633 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
634 if (cur_channel->state != CHANNEL_OPENED_STATE)
635 continue;
636 vmbus_close_internal(cur_channel);
637 }
638 /*
639 * Now close the primary.
640 */
641 vmbus_close_internal(channel);
642}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700643EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700644
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800645int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
Stephen Hemminger34543232017-02-05 17:20:34 -0700646 u32 bufferlen, u64 requestid,
647 enum vmbus_packet_type type, u32 flags)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700648{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700649 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700650 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100651 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800652 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700653 u64 aligned_data = 0;
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700654 int num_vecs = ((bufferlen != 0) ? 3 : 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700655
Hank Janssen3e7ee492009-07-13 16:02:34 -0700656
Bill Pemberton454f18a2009-07-27 16:47:24 -0400657 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800658 desc.type = type; /* VmbusPacketTypeDataInBand; */
659 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700660 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800661 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
662 desc.len8 = (u16)(packetlen_aligned >> 3);
663 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700664
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800665 bufferlist[0].iov_base = &desc;
666 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
667 bufferlist[1].iov_base = buffer;
668 bufferlist[1].iov_len = bufferlen;
669 bufferlist[2].iov_base = &aligned_data;
670 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700671
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700672 return hv_ringbuffer_write(channel, bufferlist, num_vecs);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700673}
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800674EXPORT_SYMBOL(vmbus_sendpacket_ctl);
675
676/**
677 * vmbus_sendpacket() - Send the specified buffer on the given channel
678 * @channel: Pointer to vmbus_channel structure.
679 * @buffer: Pointer to the buffer you want to receive the data into.
680 * @bufferlen: Maximum size of what the the buffer will hold
681 * @requestid: Identifier of the request
682 * @type: Type of packet that is being send e.g. negotiate, time
683 * packet etc.
684 *
685 * Sends data in @buffer directly to hyper-v via the vmbus
686 * This will send the data unparsed to hyper-v.
687 *
688 * Mainly used by Hyper-V drivers.
689 */
690int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
691 u32 bufferlen, u64 requestid,
692 enum vmbus_packet_type type, u32 flags)
693{
694 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
Stephen Hemminger34543232017-02-05 17:20:34 -0700695 type, flags);
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800696}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700697EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700698
Hank Janssen3e189512010-03-04 22:11:00 +0000699/*
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800700 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
701 * packets using a GPADL Direct packet type. This interface allows you
702 * to control notifying the host. This will be useful for sending
703 * batched data. Also the sender can control the send flags
704 * explicitly.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700705 */
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800706int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
Stephen Hemminger34543232017-02-05 17:20:34 -0700707 struct hv_page_buffer pagebuffers[],
708 u32 pagecount, void *buffer, u32 bufferlen,
709 u64 requestid, u32 flags)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700710{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700711 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000712 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700713 u32 descsize;
714 u32 packetlen;
715 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800716 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700717 u64 aligned_data = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700718
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700719 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400720 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700721
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700722 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000723 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700724 * largest size we support
725 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700726 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
727 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700728 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700729 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100730 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700731
Bill Pemberton454f18a2009-07-27 16:47:24 -0400732 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800733 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800734 desc.flags = flags;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700735 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
736 desc.length8 = (u16)(packetlen_aligned >> 3);
737 desc.transactionid = requestid;
738 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700739
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700740 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800741 desc.range[i].len = pagebuffers[i].len;
742 desc.range[i].offset = pagebuffers[i].offset;
743 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700744 }
745
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800746 bufferlist[0].iov_base = &desc;
747 bufferlist[0].iov_len = descsize;
748 bufferlist[1].iov_base = buffer;
749 bufferlist[1].iov_len = bufferlen;
750 bufferlist[2].iov_base = &aligned_data;
751 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700752
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700753 return hv_ringbuffer_write(channel, bufferlist, 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700754}
K. Y. Srinivasanb3a19b32015-03-18 12:29:29 -0700755EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800756
757/*
758 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
759 * packets using a GPADL Direct packet type.
760 */
761int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
762 struct hv_page_buffer pagebuffers[],
763 u32 pagecount, void *buffer, u32 bufferlen,
764 u64 requestid)
765{
766 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Stephen Hemminger34543232017-02-05 17:20:34 -0700767
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800768 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
Stephen Hemminger34543232017-02-05 17:20:34 -0700769 buffer, bufferlen,
770 requestid, flags);
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800771
772}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700773EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700774
Hank Janssen3e189512010-03-04 22:11:00 +0000775/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700776 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000777 * using a GPADL Direct packet type.
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800778 * The buffer includes the vmbus descriptor.
779 */
780int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
781 struct vmbus_packet_mpb_array *desc,
782 u32 desc_size,
783 void *buffer, u32 bufferlen, u64 requestid)
784{
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800785 u32 packetlen;
786 u32 packetlen_aligned;
787 struct kvec bufferlist[3];
788 u64 aligned_data = 0;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800789
790 packetlen = desc_size + bufferlen;
791 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
792
793 /* Setup the descriptor */
794 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
795 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
796 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
797 desc->length8 = (u16)(packetlen_aligned >> 3);
798 desc->transactionid = requestid;
799 desc->rangecount = 1;
800
801 bufferlist[0].iov_base = desc;
802 bufferlist[0].iov_len = desc_size;
803 bufferlist[1].iov_base = buffer;
804 bufferlist[1].iov_len = bufferlen;
805 bufferlist[2].iov_base = &aligned_data;
806 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
807
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700808 return hv_ringbuffer_write(channel, bufferlist, 3);
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800809}
810EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
811
812/*
813 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
814 * using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700815 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700816int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700817 struct hv_multipage_buffer *multi_pagebuffer,
818 void *buffer, u32 bufferlen, u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700819{
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000820 struct vmbus_channel_packet_multipage_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700821 u32 descsize;
822 u32 packetlen;
823 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800824 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700825 u64 aligned_data = 0;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800826 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
827 multi_pagebuffer->len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700828
Tobias Klauser24b8a402014-04-25 17:03:50 +0200829 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400830 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700831
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700832 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000833 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700834 * the largest size we support
835 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700836 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
837 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700838 sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700839 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100840 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700841
Hank Janssen3e7ee492009-07-13 16:02:34 -0700842
Bill Pemberton454f18a2009-07-27 16:47:24 -0400843 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800844 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000845 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700846 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
847 desc.length8 = (u16)(packetlen_aligned >> 3);
848 desc.transactionid = requestid;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000849 desc.rangecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700850
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800851 desc.range.len = multi_pagebuffer->len;
852 desc.range.offset = multi_pagebuffer->offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700853
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800854 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700855 pfncount * sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700856
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800857 bufferlist[0].iov_base = &desc;
858 bufferlist[0].iov_len = descsize;
859 bufferlist[1].iov_base = buffer;
860 bufferlist[1].iov_len = bufferlen;
861 bufferlist[2].iov_base = &aligned_data;
862 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700863
Stephen Hemminger5529eaf2017-02-11 23:02:22 -0700864 return hv_ringbuffer_write(channel, bufferlist, 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700865}
Greg Kroah-Hartman4cb106f2010-10-21 09:23:59 -0700866EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700867
868/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700869 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700870 * @channel: Pointer to vmbus_channel structure.
871 * @buffer: Pointer to the buffer you want to receive the data into.
872 * @bufferlen: Maximum size of what the the buffer will hold
873 * @buffer_actual_len: The actual size of the data after it was received
874 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700875 *
876 * Receives directly from the hyper-v vmbus and puts the data it received
877 * into Buffer. This will receive the data unparsed from hyper-v.
878 *
879 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700880 */
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800881static inline int
882__vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
883 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
884 bool raw)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700885{
K. Y. Srinivasan33725922016-11-06 13:14:18 -0800886 return hv_ringbuffer_read(channel, buffer, bufferlen,
887 buffer_actual_len, requestid, raw);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700888
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800889}
890
891int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
892 u32 bufferlen, u32 *buffer_actual_len,
893 u64 *requestid)
894{
895 return __vmbus_recvpacket(channel, buffer, bufferlen,
896 buffer_actual_len, requestid, false);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700897}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700898EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700899
Hank Janssen3e189512010-03-04 22:11:00 +0000900/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700901 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700902 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700903int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700904 u32 bufferlen, u32 *buffer_actual_len,
905 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700906{
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800907 return __vmbus_recvpacket(channel, buffer, bufferlen,
908 buffer_actual_len, requestid, true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700909}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700910EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);