blob: 56dd261f7142f3957aef255ffaec43ce38a05ee7 [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 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070042static void 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
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080046 if (channel->offermsg.monitor_allocated) {
Bill Pemberton454f18a2009-07-27 16:47:24 -040047 /* Each u32 represents 32 channels */
Olaf Hering22356582011-03-21 14:41:37 +010048 sync_set_bit(channel->offermsg.child_relid & 31,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080049 (unsigned long *) vmbus_connection.send_int_page +
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080050 (channel->offermsg.child_relid >> 5));
Hank Janssen3e7ee492009-07-13 16:02:34 -070051
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -070052 /* Get the child to parent monitor page */
53 monitorpage = vmbus_connection.monitor_pages[1];
Hank Janssen3e7ee492009-07-13 16:02:34 -070054
Olaf Hering22356582011-03-21 14:41:37 +010055 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080056 (unsigned long *)&monitorpage->trigger_group
57 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -040058
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070059 } else {
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -080060 vmbus_set_event(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -070061 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070062}
63
Hank Janssen3e189512010-03-04 22:11:00 +000064/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070065 * vmbus_open - Open the specified channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070066 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070067int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -070068 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
69 void (*onchannelcallback)(void *context), void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -070070{
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -070071 struct vmbus_channel_open_channel *open_msg;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -070072 struct vmbus_channel_msginfo *open_info = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -070073 void *in, *out;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070074 unsigned long flags;
Nicholas Mc Guire08a95132015-02-27 11:26:01 -080075 int ret, err = 0;
76 unsigned long t;
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070077 struct page *page;
Hank Janssen3e7ee492009-07-13 16:02:34 -070078
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010079 spin_lock_irqsave(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070080 if (newchannel->state == CHANNEL_OPEN_STATE) {
81 newchannel->state = CHANNEL_OPENING_STATE;
82 } else {
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010083 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070084 return -EINVAL;
85 }
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010086 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070087
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080088 newchannel->onchannel_callback = onchannelcallback;
89 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070090
Bill Pemberton454f18a2009-07-27 16:47:24 -040091 /* Allocate the ring buffer */
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070092 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
93 GFP_KERNEL|__GFP_ZERO,
94 get_order(send_ringbuffer_size +
95 recv_ringbuffer_size));
96
97 if (!page)
98 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
99 get_order(send_ringbuffer_size +
100 recv_ringbuffer_size));
101 else
102 out = (void *)page_address(page);
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800103
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800104 if (!out) {
105 err = -ENOMEM;
106 goto error0;
107 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700108
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700109 in = (void *)((unsigned long)out + send_ringbuffer_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700110
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800111 newchannel->ringbuffer_pages = out;
112 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
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700115 ret = hv_ringbuffer_init(
116 &newchannel->outbound, out, send_ringbuffer_size);
117
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000118 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400119 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700120 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400121 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700122
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700123 ret = hv_ringbuffer_init(
124 &newchannel->inbound, in, recv_ringbuffer_size);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000125 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400126 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700127 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400128 }
129
Hank Janssen3e7ee492009-07-13 16:02:34 -0700130
Bill Pemberton454f18a2009-07-27 16:47:24 -0400131 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800132 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700133
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700134 ret = vmbus_establish_gpadl(newchannel,
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800135 newchannel->outbound.ring_buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700136 send_ringbuffer_size +
137 recv_ringbuffer_size,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800138 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400139
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000140 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400141 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700142 goto error0;
Bill Pembertonb94ef342010-05-05 15:27:37 -0400143 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700144
Bill Pemberton454f18a2009-07-27 16:47:24 -0400145 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700146 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700147 sizeof(struct vmbus_channel_open_channel),
148 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700149 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400150 err = -ENOMEM;
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800151 goto error_gpadl;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400152 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700153
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700154 init_completion(&open_info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700155
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700156 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700157 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
158 open_msg->openid = newchannel->offermsg.child_relid;
159 open_msg->child_relid = newchannel->offermsg.child_relid;
160 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
161 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700162 PAGE_SHIFT;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800163 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700164
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700165 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400166 err = -EINVAL;
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800167 goto error_gpadl;
Bill Pembertonc827f942010-05-05 15:27:38 -0400168 }
169
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700170 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700171 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700172
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800173 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700174 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800175 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800176 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700177
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700178 ret = vmbus_post_msg(open_msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700179 sizeof(struct vmbus_channel_open_channel));
Hank Janssen98e08702011-03-29 13:58:44 -0700180
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700181 if (ret != 0) {
182 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700183 goto error1;
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700184 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700185
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700186 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700187 if (t == 0) {
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800188 err = -ETIMEDOUT;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700189 goto error1;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800190 }
191
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800192 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700193 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800194 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700195
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700196 if (open_info->response.open_result.status) {
197 err = -EAGAIN;
198 goto error_gpadl;
199 }
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700200
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700201 newchannel->state = CHANNEL_OPENED_STATE;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700202 kfree(open_info);
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700203 return 0;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400204
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700205error1:
206 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
207 list_del(&open_info->msglistentry);
208 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
209
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800210error_gpadl:
211 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
212
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700213error0:
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800214 free_pages((unsigned long)out,
215 get_order(send_ringbuffer_size + recv_ringbuffer_size));
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700216 kfree(open_info);
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800217 newchannel->state = CHANNEL_OPEN_STATE;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400218 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700219}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700220EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700221
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800222/* Used for Hyper-V Socket: a guest client's connect() to the host */
223int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
224 const uuid_le *shv_host_servie_id)
225{
226 struct vmbus_channel_tl_connect_request conn_msg;
227
228 memset(&conn_msg, 0, sizeof(conn_msg));
229 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
230 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
231 conn_msg.host_service_id = *shv_host_servie_id;
232
233 return vmbus_post_msg(&conn_msg, sizeof(conn_msg));
234}
235EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
236
Hank Janssen3e189512010-03-04 22:11:00 +0000237/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700238 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700239 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700240static int create_gpadl_header(void *kbuffer, u32 size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700241 struct vmbus_channel_msginfo **msginfo,
242 u32 *messagecount)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700243{
244 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700245 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700246 struct vmbus_channel_gpadl_header *gpadl_header;
247 struct vmbus_channel_gpadl_body *gpadl_body;
248 struct vmbus_channel_msginfo *msgheader;
249 struct vmbus_channel_msginfo *msgbody = NULL;
250 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700251
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700252 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700253
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700254 pagecount = size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700255
Bill Pemberton454f18a2009-07-27 16:47:24 -0400256 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700257 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700258 sizeof(struct vmbus_channel_gpadl_header) -
259 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700260 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700261
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700262 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700263 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400264 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700265 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700266 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700267 sizeof(struct gpa_range) + pfncount * sizeof(u64);
268 msgheader = kzalloc(msgsize, GFP_KERNEL);
269 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400270 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700271
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800272 INIT_LIST_HEAD(&msgheader->submsglist);
273 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700274
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700275 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800276 msgheader->msg;
277 gpadl_header->rangecount = 1;
278 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700279 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800280 gpadl_header->range[0].byte_offset = 0;
281 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700282 for (i = 0; i < pfncount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800283 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
284 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700285 *msginfo = msgheader;
286 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700287
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700288 pfnsum = pfncount;
289 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700290
Bill Pemberton454f18a2009-07-27 16:47:24 -0400291 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700292 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700293 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700294 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700295
Bill Pemberton454f18a2009-07-27 16:47:24 -0400296 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700297 while (pfnleft) {
298 if (pfnleft > pfncount)
299 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700300 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700301 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700302
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700303 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700304 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700305 pfncurr * sizeof(u64);
306 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700307
308 if (!msgbody) {
309 struct vmbus_channel_msginfo *pos = NULL;
310 struct vmbus_channel_msginfo *tmp = NULL;
311 /*
312 * Free up all the allocated messages.
313 */
314 list_for_each_entry_safe(pos, tmp,
315 &msgheader->submsglist,
316 msglistentry) {
317
318 list_del(&pos->msglistentry);
319 kfree(pos);
320 }
321
Bill Pembertond1c250b2010-05-05 15:27:35 -0400322 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700323 }
324
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800325 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700326 (*messagecount)++;
327 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;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800355 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700356
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700357 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800358 msgheader->msg;
359 gpadl_header->rangecount = 1;
360 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700361 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800362 gpadl_header->range[0].byte_offset = 0;
363 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700364 for (i = 0; i < pagecount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800365 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
366 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700367
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700368 *msginfo = msgheader;
369 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700370 }
371
372 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400373nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700374 kfree(msgheader);
375 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400376 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700377}
378
Hank Janssen3e189512010-03-04 22:11:00 +0000379/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700380 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700381 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700382 * @channel: a channel
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800383 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700384 * @size: page-size multiple
385 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700386 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700387int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700388 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700389{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700390 struct vmbus_channel_gpadl_header *gpadlmsg;
391 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700392 struct vmbus_channel_msginfo *msginfo = NULL;
393 struct vmbus_channel_msginfo *submsginfo;
394 u32 msgcount;
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
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700403 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
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
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700426 if (msgcount > 1) {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800427 list_for_each(curr, &msginfo->submsglist) {
Bill Pemberton53af5452009-09-11 21:46:44 -0400428
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700429 submsginfo = (struct vmbus_channel_msginfo *)curr;
430 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800431 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700432
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800433 gpadl_body->header.msgtype =
434 CHANNELMSG_GPADL_BODY;
435 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700436
Haiyang Zhangc6977672011-01-26 12:12:08 -0800437 ret = vmbus_post_msg(gpadl_body,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800438 submsginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700439 sizeof(*submsginfo));
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000440 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700441 goto cleanup;
Bill Pemberton99259152010-05-05 15:27:36 -0400442
Hank Janssen3e7ee492009-07-13 16:02:34 -0700443 }
444 }
K. Y. Srinivasan72c6b712014-08-27 16:25:34 -0700445 wait_for_completion(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700446
Bill Pemberton454f18a2009-07-27 16:47:24 -0400447 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800448 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700449
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700450cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800451 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800452 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800453 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700454
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700455 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700456 return ret;
457}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700458EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700459
Hank Janssen3e189512010-03-04 22:11:00 +0000460/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700461 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700462 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700463int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700464{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700465 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700466 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700467 unsigned long flags;
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700468 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700469
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700470 info = kmalloc(sizeof(*info) +
471 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400472 if (!info)
473 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700474
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700475 init_completion(&info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700476
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800477 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700478
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800479 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
480 msg->child_relid = channel->offermsg.child_relid;
481 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700482
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800483 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800484 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800485 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800486 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc6977672011-01-26 12:12:08 -0800487 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700488 sizeof(struct vmbus_channel_gpadl_teardown));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700489
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700490 if (ret)
491 goto post_msg_err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700492
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700493 wait_for_completion(&info->waitevent);
494
495post_msg_err:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800496 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800497 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800498 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700499
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700500 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700501 return ret;
502}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700503EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700504
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700505static void reset_channel_cb(void *arg)
506{
507 struct vmbus_channel *channel = arg;
508
509 channel->onchannel_callback = NULL;
510}
511
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700512static int vmbus_close_internal(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700513{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700514 struct vmbus_channel_close_channel *msg;
Dexuan Cui63d55b22015-12-14 16:01:47 -0800515 struct tasklet_struct *tasklet;
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 */
527 tasklet = hv_context.event_dpc[channel->target_cpu];
528 tasklet_disable(tasklet);
529
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800530 /*
531 * In case a device driver's probe() fails (e.g.,
532 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
533 * rescinded later (e.g., we dynamically disble an Integrated Service
534 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
535 * here we should skip most of the below cleanup work.
536 */
537 if (channel->state != CHANNEL_OPENED_STATE) {
538 ret = -EINVAL;
539 goto out;
540 }
541
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700542 channel->state = CHANNEL_OPEN_STATE;
543 channel->sc_creation_callback = NULL;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400544 /* Stop callback and cancel the timer asap */
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700545 if (channel->target_cpu != get_cpu()) {
546 put_cpu();
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700547 smp_call_function_single(channel->target_cpu, reset_channel_cb,
548 channel, true);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700549 } else {
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700550 reset_channel_cb(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700551 put_cpu();
552 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700553
Bill Pemberton454f18a2009-07-27 16:47:24 -0400554 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700555
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700556 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700557
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800558 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
559 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700560
Haiyang Zhangc6977672011-01-26 12:12:08 -0800561 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700562
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700563 if (ret) {
564 pr_err("Close failed: close post msg return is %d\n", ret);
565 /*
566 * If we failed to post the close msg,
567 * it is perhaps better to leak memory.
568 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800569 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700570 }
571
Bill Pemberton454f18a2009-07-27 16:47:24 -0400572 /* Tear down the gpadl for the channel's ring buffer */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700573 if (channel->ringbuffer_gpadlhandle) {
574 ret = vmbus_teardown_gpadl(channel,
575 channel->ringbuffer_gpadlhandle);
576 if (ret) {
577 pr_err("Close failed: teardown gpadl return %d\n", ret);
578 /*
579 * If we failed to teardown gpadl,
580 * it is perhaps better to leak memory.
581 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800582 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700583 }
584 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700585
Bill Pemberton454f18a2009-07-27 16:47:24 -0400586 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700587 hv_ringbuffer_cleanup(&channel->outbound);
588 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700589
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800590 free_pages((unsigned long)channel->ringbuffer_pages,
591 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700592
Dexuan Cui63d55b22015-12-14 16:01:47 -0800593out:
594 tasklet_enable(tasklet);
595
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700596 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700597}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700598
599/*
600 * vmbus_close - Close the specified channel
601 */
602void vmbus_close(struct vmbus_channel *channel)
603{
604 struct list_head *cur, *tmp;
605 struct vmbus_channel *cur_channel;
606
607 if (channel->primary_channel != NULL) {
608 /*
609 * We will only close sub-channels when
610 * the primary is closed.
611 */
612 return;
613 }
614 /*
615 * Close all the sub-channels first and then close the
616 * primary channel.
617 */
618 list_for_each_safe(cur, tmp, &channel->sc_list) {
619 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
620 if (cur_channel->state != CHANNEL_OPENED_STATE)
621 continue;
622 vmbus_close_internal(cur_channel);
623 }
624 /*
625 * Now close the primary.
626 */
627 vmbus_close_internal(channel);
628}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700629EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700630
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800631int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700632 u32 bufferlen, u64 requestid,
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800633 enum vmbus_packet_type type, u32 flags, bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700634{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700635 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700636 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100637 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800638 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700639 u64 aligned_data = 0;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700640 int ret;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800641 bool signal = false;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800642 bool lock = channel->acquire_ring_lock;
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700643 int num_vecs = ((bufferlen != 0) ? 3 : 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700644
Hank Janssen3e7ee492009-07-13 16:02:34 -0700645
Bill Pemberton454f18a2009-07-27 16:47:24 -0400646 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800647 desc.type = type; /* VmbusPacketTypeDataInBand; */
648 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700649 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800650 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
651 desc.len8 = (u16)(packetlen_aligned >> 3);
652 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700653
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800654 bufferlist[0].iov_base = &desc;
655 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
656 bufferlist[1].iov_base = buffer;
657 bufferlist[1].iov_len = bufferlen;
658 bufferlist[2].iov_base = &aligned_data;
659 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700660
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700661 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, num_vecs,
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800662 &signal, lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700663
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700664 /*
665 * Signalling the host is conditional on many factors:
666 * 1. The ring state changed from being empty to non-empty.
667 * This is tracked by the variable "signal".
668 * 2. The variable kick_q tracks if more data will be placed
669 * on the ring. We will not signal if more data is
670 * to be placed.
671 *
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800672 * Based on the channel signal state, we will decide
673 * which signaling policy will be applied.
674 *
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700675 * If we cannot write to the ring-buffer; signal the host
676 * even if we may not have written anything. This is a rare
677 * enough condition that it should not matter.
Dexuan Cui5f363bc2016-01-27 22:29:39 -0800678 * NOTE: in this case, the hvsock channel is an exception, because
679 * it looks the host side's hvsock implementation has a throttling
680 * mechanism which can hurt the performance otherwise.
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700681 */
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800682
683 if (channel->signal_policy)
684 signal = true;
685 else
686 kick_q = true;
687
Dexuan Cui5f363bc2016-01-27 22:29:39 -0800688 if (((ret == 0) && kick_q && signal) ||
689 (ret && !is_hvsock_channel(channel)))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700690 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700691
Hank Janssen3e7ee492009-07-13 16:02:34 -0700692 return ret;
693}
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800694EXPORT_SYMBOL(vmbus_sendpacket_ctl);
695
696/**
697 * vmbus_sendpacket() - Send the specified buffer on the given channel
698 * @channel: Pointer to vmbus_channel structure.
699 * @buffer: Pointer to the buffer you want to receive the data into.
700 * @bufferlen: Maximum size of what the the buffer will hold
701 * @requestid: Identifier of the request
702 * @type: Type of packet that is being send e.g. negotiate, time
703 * packet etc.
704 *
705 * Sends data in @buffer directly to hyper-v via the vmbus
706 * This will send the data unparsed to hyper-v.
707 *
708 * Mainly used by Hyper-V drivers.
709 */
710int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
711 u32 bufferlen, u64 requestid,
712 enum vmbus_packet_type type, u32 flags)
713{
714 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
715 type, flags, true);
716}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700717EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700718
Hank Janssen3e189512010-03-04 22:11:00 +0000719/*
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800720 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
721 * packets using a GPADL Direct packet type. This interface allows you
722 * to control notifying the host. This will be useful for sending
723 * batched data. Also the sender can control the send flags
724 * explicitly.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700725 */
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800726int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700727 struct hv_page_buffer pagebuffers[],
728 u32 pagecount, void *buffer, u32 bufferlen,
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800729 u64 requestid,
730 u32 flags,
731 bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700732{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700733 int ret;
734 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000735 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700736 u32 descsize;
737 u32 packetlen;
738 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800739 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700740 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800741 bool signal = false;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800742 bool lock = channel->acquire_ring_lock;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700743
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700744 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400745 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700746
Hank Janssen3e7ee492009-07-13 16:02:34 -0700747
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700748 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000749 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700750 * largest size we support
751 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700752 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
753 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700754 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700755 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100756 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700757
Bill Pemberton454f18a2009-07-27 16:47:24 -0400758 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800759 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800760 desc.flags = flags;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700761 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
762 desc.length8 = (u16)(packetlen_aligned >> 3);
763 desc.transactionid = requestid;
764 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700765
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700766 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800767 desc.range[i].len = pagebuffers[i].len;
768 desc.range[i].offset = pagebuffers[i].offset;
769 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700770 }
771
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800772 bufferlist[0].iov_base = &desc;
773 bufferlist[0].iov_len = descsize;
774 bufferlist[1].iov_base = buffer;
775 bufferlist[1].iov_len = bufferlen;
776 bufferlist[2].iov_base = &aligned_data;
777 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700778
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800779 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
780 &signal, lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700781
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700782 /*
783 * Signalling the host is conditional on many factors:
784 * 1. The ring state changed from being empty to non-empty.
785 * This is tracked by the variable "signal".
786 * 2. The variable kick_q tracks if more data will be placed
787 * on the ring. We will not signal if more data is
788 * to be placed.
789 *
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800790 * Based on the channel signal state, we will decide
791 * which signaling policy will be applied.
792 *
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700793 * If we cannot write to the ring-buffer; signal the host
794 * even if we may not have written anything. This is a rare
795 * enough condition that it should not matter.
796 */
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800797
798 if (channel->signal_policy)
799 signal = true;
800 else
801 kick_q = true;
802
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700803 if (((ret == 0) && kick_q && signal) || (ret))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700804 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700805
Hank Janssen3e7ee492009-07-13 16:02:34 -0700806 return ret;
807}
K. Y. Srinivasanb3a19b32015-03-18 12:29:29 -0700808EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800809
810/*
811 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
812 * packets using a GPADL Direct packet type.
813 */
814int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
815 struct hv_page_buffer pagebuffers[],
816 u32 pagecount, void *buffer, u32 bufferlen,
817 u64 requestid)
818{
819 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
820 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
821 buffer, bufferlen, requestid,
822 flags, true);
823
824}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700825EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700826
Hank Janssen3e189512010-03-04 22:11:00 +0000827/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700828 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000829 * using a GPADL Direct packet type.
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800830 * The buffer includes the vmbus descriptor.
831 */
832int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
833 struct vmbus_packet_mpb_array *desc,
834 u32 desc_size,
835 void *buffer, u32 bufferlen, u64 requestid)
836{
837 int ret;
838 u32 packetlen;
839 u32 packetlen_aligned;
840 struct kvec bufferlist[3];
841 u64 aligned_data = 0;
842 bool signal = false;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800843 bool lock = channel->acquire_ring_lock;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800844
845 packetlen = desc_size + bufferlen;
846 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
847
848 /* Setup the descriptor */
849 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
850 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
851 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
852 desc->length8 = (u16)(packetlen_aligned >> 3);
853 desc->transactionid = requestid;
854 desc->rangecount = 1;
855
856 bufferlist[0].iov_base = desc;
857 bufferlist[0].iov_len = desc_size;
858 bufferlist[1].iov_base = buffer;
859 bufferlist[1].iov_len = bufferlen;
860 bufferlist[2].iov_base = &aligned_data;
861 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
862
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800863 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
864 &signal, lock);
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800865
866 if (ret == 0 && signal)
867 vmbus_setevent(channel);
868
869 return ret;
870}
871EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
872
873/*
874 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
875 * using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700876 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700877int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700878 struct hv_multipage_buffer *multi_pagebuffer,
879 void *buffer, u32 bufferlen, u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700880{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700881 int ret;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000882 struct vmbus_channel_packet_multipage_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700883 u32 descsize;
884 u32 packetlen;
885 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800886 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700887 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800888 bool signal = false;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800889 bool lock = channel->acquire_ring_lock;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800890 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
891 multi_pagebuffer->len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700892
Tobias Klauser24b8a402014-04-25 17:03:50 +0200893 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400894 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700895
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700896 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000897 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700898 * the largest size we support
899 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700900 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
901 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700902 sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700903 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100904 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700905
Hank Janssen3e7ee492009-07-13 16:02:34 -0700906
Bill Pemberton454f18a2009-07-27 16:47:24 -0400907 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800908 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000909 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700910 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
911 desc.length8 = (u16)(packetlen_aligned >> 3);
912 desc.transactionid = requestid;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000913 desc.rangecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700914
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800915 desc.range.len = multi_pagebuffer->len;
916 desc.range.offset = multi_pagebuffer->offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700917
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800918 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700919 pfncount * sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700920
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800921 bufferlist[0].iov_base = &desc;
922 bufferlist[0].iov_len = descsize;
923 bufferlist[1].iov_base = buffer;
924 bufferlist[1].iov_len = bufferlen;
925 bufferlist[2].iov_base = &aligned_data;
926 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700927
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800928 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
929 &signal, lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700930
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800931 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700932 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700933
Hank Janssen3e7ee492009-07-13 16:02:34 -0700934 return ret;
935}
Greg Kroah-Hartman4cb106f2010-10-21 09:23:59 -0700936EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700937
938/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700939 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700940 * @channel: Pointer to vmbus_channel structure.
941 * @buffer: Pointer to the buffer you want to receive the data into.
942 * @bufferlen: Maximum size of what the the buffer will hold
943 * @buffer_actual_len: The actual size of the data after it was received
944 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700945 *
946 * Receives directly from the hyper-v vmbus and puts the data it received
947 * into Buffer. This will receive the data unparsed from hyper-v.
948 *
949 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700950 */
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800951static inline int
952__vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
953 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
954 bool raw)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700955{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700956 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800957 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700958
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800959 ret = hv_ringbuffer_read(&channel->inbound, buffer, bufferlen,
960 buffer_actual_len, requestid, &signal, raw);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700961
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800962 if (signal)
963 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700964
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800965 return ret;
966}
967
968int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
969 u32 bufferlen, u32 *buffer_actual_len,
970 u64 *requestid)
971{
972 return __vmbus_recvpacket(channel, buffer, bufferlen,
973 buffer_actual_len, requestid, false);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700974}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700975EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700976
Hank Janssen3e189512010-03-04 22:11:00 +0000977/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700978 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700979 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700980int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700981 u32 bufferlen, u32 *buffer_actual_len,
982 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700983{
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800984 return __vmbus_recvpacket(channel, buffer, bufferlen,
985 buffer_actual_len, requestid, true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700986}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700987EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);