blob: 5fb4c6d9209b753a12dcddb4c441ff00c3e8746a [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 */
50 if ((channel->offermsg.monitor_allocated) &&
51 (!channel->low_latency)) {
Bill Pemberton454f18a2009-07-27 16:47:24 -040052 /* Each u32 represents 32 channels */
Olaf Hering22356582011-03-21 14:41:37 +010053 sync_set_bit(channel->offermsg.child_relid & 31,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080054 (unsigned long *) vmbus_connection.send_int_page +
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080055 (channel->offermsg.child_relid >> 5));
Hank Janssen3e7ee492009-07-13 16:02:34 -070056
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -070057 /* Get the child to parent monitor page */
58 monitorpage = vmbus_connection.monitor_pages[1];
Hank Janssen3e7ee492009-07-13 16:02:34 -070059
Olaf Hering22356582011-03-21 14:41:37 +010060 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080061 (unsigned long *)&monitorpage->trigger_group
62 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -040063
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070064 } else {
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -080065 vmbus_set_event(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -070066 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070067}
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -080068EXPORT_SYMBOL_GPL(vmbus_setevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -070069
Hank Janssen3e189512010-03-04 22:11:00 +000070/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070071 * vmbus_open - Open the specified channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070072 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070073int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -070074 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
75 void (*onchannelcallback)(void *context), void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -070076{
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -070077 struct vmbus_channel_open_channel *open_msg;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -070078 struct vmbus_channel_msginfo *open_info = NULL;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070079 unsigned long flags;
Nicholas Mc Guire08a95132015-02-27 11:26:01 -080080 int ret, err = 0;
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070081 struct page *page;
Hank Janssen3e7ee492009-07-13 16:02:34 -070082
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -070083 if (send_ringbuffer_size % PAGE_SIZE ||
84 recv_ringbuffer_size % PAGE_SIZE)
85 return -EINVAL;
86
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010087 spin_lock_irqsave(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070088 if (newchannel->state == CHANNEL_OPEN_STATE) {
89 newchannel->state = CHANNEL_OPENING_STATE;
90 } else {
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010091 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070092 return -EINVAL;
93 }
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010094 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070095
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080096 newchannel->onchannel_callback = onchannelcallback;
97 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070098
Bill Pemberton454f18a2009-07-27 16:47:24 -040099 /* Allocate the ring buffer */
K. Y. Srinivasan294409d2015-05-31 21:27:03 -0700100 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
101 GFP_KERNEL|__GFP_ZERO,
102 get_order(send_ringbuffer_size +
103 recv_ringbuffer_size));
104
105 if (!page)
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700106 page = alloc_pages(GFP_KERNEL|__GFP_ZERO,
107 get_order(send_ringbuffer_size +
108 recv_ringbuffer_size));
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800109
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700110 if (!page) {
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800111 err = -ENOMEM;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700112 goto error_set_chnstate;
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800113 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700114
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700115 newchannel->ringbuffer_pages = page_address(page);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800116 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700117 recv_ringbuffer_size) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700118
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700119 ret = hv_ringbuffer_init(&newchannel->outbound, page,
120 send_ringbuffer_size >> PAGE_SHIFT);
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700121
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000122 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400123 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700124 goto error_free_pages;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400125 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700126
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700127 ret = hv_ringbuffer_init(&newchannel->inbound,
128 &page[send_ringbuffer_size >> PAGE_SHIFT],
129 recv_ringbuffer_size >> PAGE_SHIFT);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000130 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400131 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700132 goto error_free_pages;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400133 }
134
Hank Janssen3e7ee492009-07-13 16:02:34 -0700135
Bill Pemberton454f18a2009-07-27 16:47:24 -0400136 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800137 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700138
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700139 ret = vmbus_establish_gpadl(newchannel,
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700140 page_address(page),
141 send_ringbuffer_size +
142 recv_ringbuffer_size,
143 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400144
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000145 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400146 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700147 goto error_free_pages;
Bill Pembertonb94ef342010-05-05 15:27:37 -0400148 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700149
Bill Pemberton454f18a2009-07-27 16:47:24 -0400150 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700151 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700152 sizeof(struct vmbus_channel_open_channel),
153 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700154 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400155 err = -ENOMEM;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700156 goto error_free_gpadl;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400157 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700158
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700159 init_completion(&open_info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700160
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700161 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700162 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
163 open_msg->openid = newchannel->offermsg.child_relid;
164 open_msg->child_relid = newchannel->offermsg.child_relid;
165 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
166 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700167 PAGE_SHIFT;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800168 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700169
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700170 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400171 err = -EINVAL;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700172 goto error_free_gpadl;
Bill Pembertonc827f942010-05-05 15:27:38 -0400173 }
174
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700175 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700176 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700177
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800178 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700179 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800180 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800181 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700182
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700183 ret = vmbus_post_msg(open_msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700184 sizeof(struct vmbus_channel_open_channel));
Hank Janssen98e08702011-03-29 13:58:44 -0700185
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700186 if (ret != 0) {
187 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700188 goto error_clean_msglist;
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700189 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700190
Vitaly Kuznetsov396e2872016-06-09 17:08:56 -0700191 wait_for_completion(&open_info->waitevent);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800192
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800193 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700194 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800195 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700196
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700197 if (open_info->response.open_result.status) {
198 err = -EAGAIN;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700199 goto error_free_gpadl;
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700200 }
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700201
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700202 newchannel->state = CHANNEL_OPENED_STATE;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700203 kfree(open_info);
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700204 return 0;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400205
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700206error_clean_msglist:
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700207 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
208 list_del(&open_info->msglistentry);
209 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
210
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700211error_free_gpadl:
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800212 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700213 kfree(open_info);
214error_free_pages:
Vitaly Kuznetsov9988ce62016-09-02 05:58:20 -0700215 hv_ringbuffer_cleanup(&newchannel->outbound);
216 hv_ringbuffer_cleanup(&newchannel->inbound);
217 __free_pages(page,
218 get_order(send_ringbuffer_size + recv_ringbuffer_size));
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700219error_set_chnstate:
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800220 newchannel->state = CHANNEL_OPEN_STATE;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400221 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700222}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700223EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700224
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800225/* Used for Hyper-V Socket: a guest client's connect() to the host */
226int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
227 const uuid_le *shv_host_servie_id)
228{
229 struct vmbus_channel_tl_connect_request conn_msg;
230
231 memset(&conn_msg, 0, sizeof(conn_msg));
232 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
233 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
234 conn_msg.host_service_id = *shv_host_servie_id;
235
236 return vmbus_post_msg(&conn_msg, sizeof(conn_msg));
237}
238EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
239
Hank Janssen3e189512010-03-04 22:11:00 +0000240/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700241 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700242 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700243static int create_gpadl_header(void *kbuffer, u32 size,
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700244 struct vmbus_channel_msginfo **msginfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700245{
246 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700247 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700248 struct vmbus_channel_gpadl_header *gpadl_header;
249 struct vmbus_channel_gpadl_body *gpadl_body;
250 struct vmbus_channel_msginfo *msgheader;
251 struct vmbus_channel_msginfo *msgbody = NULL;
252 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700253
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700254 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700255
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700256 pagecount = size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700257
Bill Pemberton454f18a2009-07-27 16:47:24 -0400258 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700259 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700260 sizeof(struct vmbus_channel_gpadl_header) -
261 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700262 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700263
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700264 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700265 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400266 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700267 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700268 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700269 sizeof(struct gpa_range) + pfncount * sizeof(u64);
270 msgheader = kzalloc(msgsize, GFP_KERNEL);
271 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400272 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700273
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800274 INIT_LIST_HEAD(&msgheader->submsglist);
275 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700276
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700277 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800278 msgheader->msg;
279 gpadl_header->rangecount = 1;
280 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700281 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800282 gpadl_header->range[0].byte_offset = 0;
283 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700284 for (i = 0; i < pfncount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800285 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
286 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700287 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700288
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700289 pfnsum = pfncount;
290 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700291
Bill Pemberton454f18a2009-07-27 16:47:24 -0400292 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700293 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700294 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700295 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700296
Bill Pemberton454f18a2009-07-27 16:47:24 -0400297 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700298 while (pfnleft) {
299 if (pfnleft > pfncount)
300 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700301 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700302 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700303
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700304 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700305 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700306 pfncurr * sizeof(u64);
307 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700308
309 if (!msgbody) {
310 struct vmbus_channel_msginfo *pos = NULL;
311 struct vmbus_channel_msginfo *tmp = NULL;
312 /*
313 * Free up all the allocated messages.
314 */
315 list_for_each_entry_safe(pos, tmp,
316 &msgheader->submsglist,
317 msglistentry) {
318
319 list_del(&pos->msglistentry);
320 kfree(pos);
321 }
322
Bill Pembertond1c250b2010-05-05 15:27:35 -0400323 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700324 }
325
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800326 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700327 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800328 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700329
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700330 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700331 * Gpadl is u32 and we are using a pointer which could
332 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700333 * This is governed by the guest/host protocol and
334 * so the hypervisor gurantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700335 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700336 for (i = 0; i < pfncurr; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800337 gpadl_body->pfn[i] = slow_virt_to_phys(
338 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
339 PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700340
Bill Pemberton454f18a2009-07-27 16:47:24 -0400341 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800342 list_add_tail(&msgbody->msglistentry,
343 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700344 pfnsum += pfncurr;
345 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700346 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700347 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400348 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700349 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700350 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700351 sizeof(struct gpa_range) + pagecount * sizeof(u64);
352 msgheader = kzalloc(msgsize, GFP_KERNEL);
353 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400354 goto nomem;
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700355
356 INIT_LIST_HEAD(&msgheader->submsglist);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800357 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700358
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700359 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800360 msgheader->msg;
361 gpadl_header->rangecount = 1;
362 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700363 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800364 gpadl_header->range[0].byte_offset = 0;
365 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700366 for (i = 0; i < pagecount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800367 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
368 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700369
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700370 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700371 }
372
373 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400374nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700375 kfree(msgheader);
376 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400377 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700378}
379
Hank Janssen3e189512010-03-04 22:11:00 +0000380/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700381 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700382 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700383 * @channel: a channel
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800384 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700385 * @size: page-size multiple
386 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700387 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700388int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700389 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700390{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700391 struct vmbus_channel_gpadl_header *gpadlmsg;
392 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700393 struct vmbus_channel_msginfo *msginfo = NULL;
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700394 struct vmbus_channel_msginfo *submsginfo, *tmp;
Bill Pemberton53af5452009-09-11 21:46:44 -0400395 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700396 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700397 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400398 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700399
K. Y. Srinivasan9f52a162015-01-09 23:54:33 -0800400 next_gpadl_handle =
401 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700402
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700403 ret = create_gpadl_header(kbuffer, size, &msginfo);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400404 if (ret)
405 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700406
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700407 init_completion(&msginfo->waitevent);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400408
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800409 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
410 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
411 gpadlmsg->child_relid = channel->offermsg.child_relid;
412 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700413
Hank Janssen3e7ee492009-07-13 16:02:34 -0700414
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800415 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800416 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800417 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700418
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800419 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700420
Haiyang Zhangc6977672011-01-26 12:12:08 -0800421 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700422 sizeof(*msginfo));
Hank Janssen98e08702011-03-29 13:58:44 -0700423 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700424 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700425
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700426 list_for_each(curr, &msginfo->submsglist) {
427 submsginfo = (struct vmbus_channel_msginfo *)curr;
428 gpadl_body =
429 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Bill Pemberton53af5452009-09-11 21:46:44 -0400430
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700431 gpadl_body->header.msgtype =
432 CHANNELMSG_GPADL_BODY;
433 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700434
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700435 ret = vmbus_post_msg(gpadl_body,
436 submsginfo->msgsize -
437 sizeof(*submsginfo));
438 if (ret != 0)
439 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700440
Hank Janssen3e7ee492009-07-13 16:02:34 -0700441 }
K. Y. Srinivasan72c6b712014-08-27 16:25:34 -0700442 wait_for_completion(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700443
Bill Pemberton454f18a2009-07-27 16:47:24 -0400444 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800445 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700446
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700447cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800448 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800449 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800450 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700451 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
452 msglistentry) {
453 kfree(submsginfo);
454 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700455
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700456 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700457 return ret;
458}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700459EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700460
Hank Janssen3e189512010-03-04 22:11:00 +0000461/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700462 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700463 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700464int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700465{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700466 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700467 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700468 unsigned long flags;
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700469 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700470
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700471 info = kmalloc(sizeof(*info) +
472 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400473 if (!info)
474 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700475
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700476 init_completion(&info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700477
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800478 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700479
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800480 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
481 msg->child_relid = channel->offermsg.child_relid;
482 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700483
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800484 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800485 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800486 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800487 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc6977672011-01-26 12:12:08 -0800488 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700489 sizeof(struct vmbus_channel_gpadl_teardown));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700490
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700491 if (ret)
492 goto post_msg_err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700493
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700494 wait_for_completion(&info->waitevent);
495
496post_msg_err:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800497 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800498 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800499 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700500
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700501 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700502 return ret;
503}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700504EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700505
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700506static void reset_channel_cb(void *arg)
507{
508 struct vmbus_channel *channel = arg;
509
510 channel->onchannel_callback = NULL;
511}
512
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700513static int vmbus_close_internal(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700514{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700515 struct vmbus_channel_close_channel *msg;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700516 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700517
Dexuan Cui63d55b22015-12-14 16:01:47 -0800518 /*
519 * process_chn_event(), running in the tasklet, can race
520 * with vmbus_close_internal() in the case of SMP guest, e.g., when
521 * the former is accessing channel->inbound.ring_buffer, the latter
522 * could be freeing the ring_buffer pages.
523 *
524 * To resolve the race, we can serialize them by disabling the
525 * tasklet when the latter is running here.
526 */
Dexuan Cui638fea32016-06-09 18:47:24 -0700527 hv_event_tasklet_disable(channel);
Dexuan Cui63d55b22015-12-14 16:01:47 -0800528
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800529 /*
530 * In case a device driver's probe() fails (e.g.,
531 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
532 * rescinded later (e.g., we dynamically disble an Integrated Service
533 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
534 * here we should skip most of the below cleanup work.
535 */
536 if (channel->state != CHANNEL_OPENED_STATE) {
537 ret = -EINVAL;
538 goto out;
539 }
540
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700541 channel->state = CHANNEL_OPEN_STATE;
542 channel->sc_creation_callback = NULL;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400543 /* Stop callback and cancel the timer asap */
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700544 if (channel->target_cpu != get_cpu()) {
545 put_cpu();
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700546 smp_call_function_single(channel->target_cpu, reset_channel_cb,
547 channel, true);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700548 } else {
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700549 reset_channel_cb(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700550 put_cpu();
551 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700552
Bill Pemberton454f18a2009-07-27 16:47:24 -0400553 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700554
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700555 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700556
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800557 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
558 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700559
Haiyang Zhangc6977672011-01-26 12:12:08 -0800560 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700561
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700562 if (ret) {
563 pr_err("Close failed: close post msg return is %d\n", ret);
564 /*
565 * If we failed to post the close msg,
566 * it is perhaps better to leak memory.
567 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800568 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700569 }
570
Bill Pemberton454f18a2009-07-27 16:47:24 -0400571 /* Tear down the gpadl for the channel's ring buffer */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700572 if (channel->ringbuffer_gpadlhandle) {
573 ret = vmbus_teardown_gpadl(channel,
574 channel->ringbuffer_gpadlhandle);
575 if (ret) {
576 pr_err("Close failed: teardown gpadl return %d\n", ret);
577 /*
578 * If we failed to teardown gpadl,
579 * it is perhaps better to leak memory.
580 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800581 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700582 }
583 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700584
Bill Pemberton454f18a2009-07-27 16:47:24 -0400585 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700586 hv_ringbuffer_cleanup(&channel->outbound);
587 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700588
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800589 free_pages((unsigned long)channel->ringbuffer_pages,
590 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700591
Dexuan Cui63d55b22015-12-14 16:01:47 -0800592out:
Dexuan Cui638fea32016-06-09 18:47:24 -0700593 hv_event_tasklet_enable(channel);
Dexuan Cui63d55b22015-12-14 16:01:47 -0800594
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700595 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700596}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700597
598/*
599 * vmbus_close - Close the specified channel
600 */
601void vmbus_close(struct vmbus_channel *channel)
602{
603 struct list_head *cur, *tmp;
604 struct vmbus_channel *cur_channel;
605
606 if (channel->primary_channel != NULL) {
607 /*
608 * We will only close sub-channels when
609 * the primary is closed.
610 */
611 return;
612 }
613 /*
614 * Close all the sub-channels first and then close the
615 * primary channel.
616 */
617 list_for_each_safe(cur, tmp, &channel->sc_list) {
618 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
619 if (cur_channel->state != CHANNEL_OPENED_STATE)
620 continue;
621 vmbus_close_internal(cur_channel);
622 }
623 /*
624 * Now close the primary.
625 */
626 vmbus_close_internal(channel);
627}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700628EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700629
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800630int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700631 u32 bufferlen, u64 requestid,
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800632 enum vmbus_packet_type type, u32 flags, bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700633{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700634 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700635 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100636 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800637 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700638 u64 aligned_data = 0;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800639 bool lock = channel->acquire_ring_lock;
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700640 int num_vecs = ((bufferlen != 0) ? 3 : 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700641
Hank Janssen3e7ee492009-07-13 16:02:34 -0700642
Bill Pemberton454f18a2009-07-27 16:47:24 -0400643 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800644 desc.type = type; /* VmbusPacketTypeDataInBand; */
645 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700646 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800647 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
648 desc.len8 = (u16)(packetlen_aligned >> 3);
649 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700650
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800651 bufferlist[0].iov_base = &desc;
652 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
653 bufferlist[1].iov_base = buffer;
654 bufferlist[1].iov_len = bufferlen;
655 bufferlist[2].iov_base = &aligned_data;
656 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700657
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -0800658 return hv_ringbuffer_write(channel, bufferlist, num_vecs,
659 lock, kick_q);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700660
Hank Janssen3e7ee492009-07-13 16:02:34 -0700661}
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800662EXPORT_SYMBOL(vmbus_sendpacket_ctl);
663
664/**
665 * vmbus_sendpacket() - Send the specified buffer on the given channel
666 * @channel: Pointer to vmbus_channel structure.
667 * @buffer: Pointer to the buffer you want to receive the data into.
668 * @bufferlen: Maximum size of what the the buffer will hold
669 * @requestid: Identifier of the request
670 * @type: Type of packet that is being send e.g. negotiate, time
671 * packet etc.
672 *
673 * Sends data in @buffer directly to hyper-v via the vmbus
674 * This will send the data unparsed to hyper-v.
675 *
676 * Mainly used by Hyper-V drivers.
677 */
678int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
679 u32 bufferlen, u64 requestid,
680 enum vmbus_packet_type type, u32 flags)
681{
682 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
683 type, flags, true);
684}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700685EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700686
Hank Janssen3e189512010-03-04 22:11:00 +0000687/*
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800688 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
689 * packets using a GPADL Direct packet type. This interface allows you
690 * to control notifying the host. This will be useful for sending
691 * batched data. Also the sender can control the send flags
692 * explicitly.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700693 */
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800694int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700695 struct hv_page_buffer pagebuffers[],
696 u32 pagecount, void *buffer, u32 bufferlen,
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800697 u64 requestid,
698 u32 flags,
699 bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700700{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700701 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000702 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700703 u32 descsize;
704 u32 packetlen;
705 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800706 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700707 u64 aligned_data = 0;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800708 bool lock = channel->acquire_ring_lock;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700709
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700710 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400711 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700712
Hank Janssen3e7ee492009-07-13 16:02:34 -0700713
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700714 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000715 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700716 * largest size we support
717 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700718 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
719 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700720 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700721 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100722 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700723
Bill Pemberton454f18a2009-07-27 16:47:24 -0400724 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800725 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800726 desc.flags = flags;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700727 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
728 desc.length8 = (u16)(packetlen_aligned >> 3);
729 desc.transactionid = requestid;
730 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700731
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700732 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800733 desc.range[i].len = pagebuffers[i].len;
734 desc.range[i].offset = pagebuffers[i].offset;
735 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700736 }
737
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800738 bufferlist[0].iov_base = &desc;
739 bufferlist[0].iov_len = descsize;
740 bufferlist[1].iov_base = buffer;
741 bufferlist[1].iov_len = bufferlen;
742 bufferlist[2].iov_base = &aligned_data;
743 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700744
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -0800745 return hv_ringbuffer_write(channel, bufferlist, 3,
746 lock, kick_q);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700747}
K. Y. Srinivasanb3a19b32015-03-18 12:29:29 -0700748EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800749
750/*
751 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
752 * packets using a GPADL Direct packet type.
753 */
754int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
755 struct hv_page_buffer pagebuffers[],
756 u32 pagecount, void *buffer, u32 bufferlen,
757 u64 requestid)
758{
759 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
760 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
761 buffer, bufferlen, requestid,
762 flags, true);
763
764}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700765EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700766
Hank Janssen3e189512010-03-04 22:11:00 +0000767/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700768 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000769 * using a GPADL Direct packet type.
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800770 * The buffer includes the vmbus descriptor.
771 */
772int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
773 struct vmbus_packet_mpb_array *desc,
774 u32 desc_size,
775 void *buffer, u32 bufferlen, u64 requestid)
776{
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800777 u32 packetlen;
778 u32 packetlen_aligned;
779 struct kvec bufferlist[3];
780 u64 aligned_data = 0;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800781 bool lock = channel->acquire_ring_lock;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800782
783 packetlen = desc_size + bufferlen;
784 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
785
786 /* Setup the descriptor */
787 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
788 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
789 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
790 desc->length8 = (u16)(packetlen_aligned >> 3);
791 desc->transactionid = requestid;
792 desc->rangecount = 1;
793
794 bufferlist[0].iov_base = desc;
795 bufferlist[0].iov_len = desc_size;
796 bufferlist[1].iov_base = buffer;
797 bufferlist[1].iov_len = bufferlen;
798 bufferlist[2].iov_base = &aligned_data;
799 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
800
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -0800801 return hv_ringbuffer_write(channel, bufferlist, 3,
802 lock, true);
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800803}
804EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
805
806/*
807 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
808 * using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700809 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700810int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700811 struct hv_multipage_buffer *multi_pagebuffer,
812 void *buffer, u32 bufferlen, u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700813{
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000814 struct vmbus_channel_packet_multipage_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700815 u32 descsize;
816 u32 packetlen;
817 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800818 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700819 u64 aligned_data = 0;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800820 bool lock = channel->acquire_ring_lock;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800821 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
822 multi_pagebuffer->len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700823
Tobias Klauser24b8a402014-04-25 17:03:50 +0200824 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400825 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700826
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700827 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000828 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700829 * the largest size we support
830 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700831 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
832 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700833 sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700834 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100835 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700836
Hank Janssen3e7ee492009-07-13 16:02:34 -0700837
Bill Pemberton454f18a2009-07-27 16:47:24 -0400838 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800839 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000840 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700841 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
842 desc.length8 = (u16)(packetlen_aligned >> 3);
843 desc.transactionid = requestid;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000844 desc.rangecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700845
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800846 desc.range.len = multi_pagebuffer->len;
847 desc.range.offset = multi_pagebuffer->offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700848
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800849 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700850 pfncount * sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700851
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800852 bufferlist[0].iov_base = &desc;
853 bufferlist[0].iov_len = descsize;
854 bufferlist[1].iov_base = buffer;
855 bufferlist[1].iov_len = bufferlen;
856 bufferlist[2].iov_base = &aligned_data;
857 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700858
K. Y. Srinivasan1f6ee4e2016-11-06 13:14:17 -0800859 return hv_ringbuffer_write(channel, bufferlist, 3,
860 lock, true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700861}
Greg Kroah-Hartman4cb106f2010-10-21 09:23:59 -0700862EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700863
864/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700865 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700866 * @channel: Pointer to vmbus_channel structure.
867 * @buffer: Pointer to the buffer you want to receive the data into.
868 * @bufferlen: Maximum size of what the the buffer will hold
869 * @buffer_actual_len: The actual size of the data after it was received
870 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700871 *
872 * Receives directly from the hyper-v vmbus and puts the data it received
873 * into Buffer. This will receive the data unparsed from hyper-v.
874 *
875 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700876 */
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800877static inline int
878__vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
879 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
880 bool raw)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700881{
K. Y. Srinivasan33725922016-11-06 13:14:18 -0800882 return hv_ringbuffer_read(channel, buffer, bufferlen,
883 buffer_actual_len, requestid, raw);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700884
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800885}
886
887int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
888 u32 bufferlen, u32 *buffer_actual_len,
889 u64 *requestid)
890{
891 return __vmbus_recvpacket(channel, buffer, bufferlen,
892 buffer_actual_len, requestid, false);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700893}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700894EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700895
Hank Janssen3e189512010-03-04 22:11:00 +0000896/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700897 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700898 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700899int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700900 u32 bufferlen, u32 *buffer_actual_len,
901 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700902{
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800903 return __vmbus_recvpacket(channel, buffer, bufferlen,
904 buffer_actual_len, requestid, true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700905}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700906EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);