blob: e47d37d21d56d56b0990084eb36868909829b425 [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;
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070076 struct page *page;
Hank Janssen3e7ee492009-07-13 16:02:34 -070077
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010078 spin_lock_irqsave(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070079 if (newchannel->state == CHANNEL_OPEN_STATE) {
80 newchannel->state = CHANNEL_OPENING_STATE;
81 } else {
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010082 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070083 return -EINVAL;
84 }
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010085 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070086
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080087 newchannel->onchannel_callback = onchannelcallback;
88 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070089
Bill Pemberton454f18a2009-07-27 16:47:24 -040090 /* Allocate the ring buffer */
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070091 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
92 GFP_KERNEL|__GFP_ZERO,
93 get_order(send_ringbuffer_size +
94 recv_ringbuffer_size));
95
96 if (!page)
97 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
98 get_order(send_ringbuffer_size +
99 recv_ringbuffer_size));
100 else
101 out = (void *)page_address(page);
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800102
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800103 if (!out) {
104 err = -ENOMEM;
105 goto error0;
106 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700107
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700108 in = (void *)((unsigned long)out + send_ringbuffer_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700109
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800110 newchannel->ringbuffer_pages = out;
111 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700112 recv_ringbuffer_size) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700113
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700114 ret = hv_ringbuffer_init(
115 &newchannel->outbound, out, send_ringbuffer_size);
116
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000117 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400118 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700119 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400120 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700121
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700122 ret = hv_ringbuffer_init(
123 &newchannel->inbound, in, recv_ringbuffer_size);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000124 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400125 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700126 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400127 }
128
Hank Janssen3e7ee492009-07-13 16:02:34 -0700129
Bill Pemberton454f18a2009-07-27 16:47:24 -0400130 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800131 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700132
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700133 ret = vmbus_establish_gpadl(newchannel,
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800134 newchannel->outbound.ring_buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700135 send_ringbuffer_size +
136 recv_ringbuffer_size,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800137 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400138
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000139 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400140 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700141 goto error0;
Bill Pembertonb94ef342010-05-05 15:27:37 -0400142 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700143
Bill Pemberton454f18a2009-07-27 16:47:24 -0400144 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700145 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700146 sizeof(struct vmbus_channel_open_channel),
147 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700148 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400149 err = -ENOMEM;
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800150 goto error_gpadl;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400151 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700152
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700153 init_completion(&open_info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700154
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700155 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700156 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
157 open_msg->openid = newchannel->offermsg.child_relid;
158 open_msg->child_relid = newchannel->offermsg.child_relid;
159 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
160 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700161 PAGE_SHIFT;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800162 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700163
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700164 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400165 err = -EINVAL;
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800166 goto error_gpadl;
Bill Pembertonc827f942010-05-05 15:27:38 -0400167 }
168
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700169 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700170 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700171
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800172 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700173 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800174 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800175 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700176
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700177 ret = vmbus_post_msg(open_msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700178 sizeof(struct vmbus_channel_open_channel));
Hank Janssen98e08702011-03-29 13:58:44 -0700179
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700180 if (ret != 0) {
181 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700182 goto error1;
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700183 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700184
Vitaly Kuznetsov396e2872016-06-09 17:08:56 -0700185 wait_for_completion(&open_info->waitevent);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800186
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800187 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700188 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800189 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700190
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700191 if (open_info->response.open_result.status) {
192 err = -EAGAIN;
193 goto error_gpadl;
194 }
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700195
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700196 newchannel->state = CHANNEL_OPENED_STATE;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700197 kfree(open_info);
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700198 return 0;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400199
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700200error1:
201 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
202 list_del(&open_info->msglistentry);
203 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
204
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800205error_gpadl:
206 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
207
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700208error0:
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800209 free_pages((unsigned long)out,
210 get_order(send_ringbuffer_size + recv_ringbuffer_size));
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700211 kfree(open_info);
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800212 newchannel->state = CHANNEL_OPEN_STATE;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400213 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700214}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700215EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700216
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800217/* Used for Hyper-V Socket: a guest client's connect() to the host */
218int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
219 const uuid_le *shv_host_servie_id)
220{
221 struct vmbus_channel_tl_connect_request conn_msg;
222
223 memset(&conn_msg, 0, sizeof(conn_msg));
224 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
225 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
226 conn_msg.host_service_id = *shv_host_servie_id;
227
228 return vmbus_post_msg(&conn_msg, sizeof(conn_msg));
229}
230EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
231
Hank Janssen3e189512010-03-04 22:11:00 +0000232/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700233 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700234 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700235static int create_gpadl_header(void *kbuffer, u32 size,
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700236 struct vmbus_channel_msginfo **msginfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700237{
238 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700239 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700240 struct vmbus_channel_gpadl_header *gpadl_header;
241 struct vmbus_channel_gpadl_body *gpadl_body;
242 struct vmbus_channel_msginfo *msgheader;
243 struct vmbus_channel_msginfo *msgbody = NULL;
244 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700245
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700246 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700247
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700248 pagecount = size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700249
Bill Pemberton454f18a2009-07-27 16:47:24 -0400250 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700251 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700252 sizeof(struct vmbus_channel_gpadl_header) -
253 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700254 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700255
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700256 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700257 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400258 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700259 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700260 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700261 sizeof(struct gpa_range) + pfncount * sizeof(u64);
262 msgheader = kzalloc(msgsize, GFP_KERNEL);
263 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400264 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700265
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800266 INIT_LIST_HEAD(&msgheader->submsglist);
267 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700268
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700269 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800270 msgheader->msg;
271 gpadl_header->rangecount = 1;
272 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700273 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800274 gpadl_header->range[0].byte_offset = 0;
275 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700276 for (i = 0; i < pfncount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800277 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
278 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700279 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700280
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700281 pfnsum = pfncount;
282 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700283
Bill Pemberton454f18a2009-07-27 16:47:24 -0400284 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700285 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700286 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700287 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700288
Bill Pemberton454f18a2009-07-27 16:47:24 -0400289 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700290 while (pfnleft) {
291 if (pfnleft > pfncount)
292 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700293 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700294 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700295
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700296 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700297 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700298 pfncurr * sizeof(u64);
299 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700300
301 if (!msgbody) {
302 struct vmbus_channel_msginfo *pos = NULL;
303 struct vmbus_channel_msginfo *tmp = NULL;
304 /*
305 * Free up all the allocated messages.
306 */
307 list_for_each_entry_safe(pos, tmp,
308 &msgheader->submsglist,
309 msglistentry) {
310
311 list_del(&pos->msglistentry);
312 kfree(pos);
313 }
314
Bill Pembertond1c250b2010-05-05 15:27:35 -0400315 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700316 }
317
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800318 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700319 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800320 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700321
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700322 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700323 * Gpadl is u32 and we are using a pointer which could
324 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700325 * This is governed by the guest/host protocol and
326 * so the hypervisor gurantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700327 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700328 for (i = 0; i < pfncurr; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800329 gpadl_body->pfn[i] = slow_virt_to_phys(
330 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
331 PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700332
Bill Pemberton454f18a2009-07-27 16:47:24 -0400333 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800334 list_add_tail(&msgbody->msglistentry,
335 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700336 pfnsum += pfncurr;
337 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700338 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700339 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400340 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700341 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700342 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700343 sizeof(struct gpa_range) + pagecount * sizeof(u64);
344 msgheader = kzalloc(msgsize, GFP_KERNEL);
345 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400346 goto nomem;
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700347
348 INIT_LIST_HEAD(&msgheader->submsglist);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800349 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700350
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700351 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800352 msgheader->msg;
353 gpadl_header->rangecount = 1;
354 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700355 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800356 gpadl_header->range[0].byte_offset = 0;
357 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700358 for (i = 0; i < pagecount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800359 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
360 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700361
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700362 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700363 }
364
365 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400366nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700367 kfree(msgheader);
368 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400369 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700370}
371
Hank Janssen3e189512010-03-04 22:11:00 +0000372/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700373 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700374 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700375 * @channel: a channel
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800376 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700377 * @size: page-size multiple
378 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700379 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700380int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700381 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700382{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700383 struct vmbus_channel_gpadl_header *gpadlmsg;
384 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700385 struct vmbus_channel_msginfo *msginfo = NULL;
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700386 struct vmbus_channel_msginfo *submsginfo, *tmp;
Bill Pemberton53af5452009-09-11 21:46:44 -0400387 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700388 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700389 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400390 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700391
K. Y. Srinivasan9f52a162015-01-09 23:54:33 -0800392 next_gpadl_handle =
393 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700394
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700395 ret = create_gpadl_header(kbuffer, size, &msginfo);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400396 if (ret)
397 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700398
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700399 init_completion(&msginfo->waitevent);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400400
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800401 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
402 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
403 gpadlmsg->child_relid = channel->offermsg.child_relid;
404 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700405
Hank Janssen3e7ee492009-07-13 16:02:34 -0700406
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800407 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800408 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800409 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700410
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800411 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700412
Haiyang Zhangc6977672011-01-26 12:12:08 -0800413 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700414 sizeof(*msginfo));
Hank Janssen98e08702011-03-29 13:58:44 -0700415 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700416 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700417
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700418 list_for_each(curr, &msginfo->submsglist) {
419 submsginfo = (struct vmbus_channel_msginfo *)curr;
420 gpadl_body =
421 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Bill Pemberton53af5452009-09-11 21:46:44 -0400422
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700423 gpadl_body->header.msgtype =
424 CHANNELMSG_GPADL_BODY;
425 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700426
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700427 ret = vmbus_post_msg(gpadl_body,
428 submsginfo->msgsize -
429 sizeof(*submsginfo));
430 if (ret != 0)
431 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700432
Hank Janssen3e7ee492009-07-13 16:02:34 -0700433 }
K. Y. Srinivasan72c6b712014-08-27 16:25:34 -0700434 wait_for_completion(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700435
Bill Pemberton454f18a2009-07-27 16:47:24 -0400436 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800437 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700438
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700439cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800440 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800441 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800442 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700443 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
444 msglistentry) {
445 kfree(submsginfo);
446 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700447
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700448 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700449 return ret;
450}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700451EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700452
Hank Janssen3e189512010-03-04 22:11:00 +0000453/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700454 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700455 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700456int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700457{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700458 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700459 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700460 unsigned long flags;
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700461 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700462
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700463 info = kmalloc(sizeof(*info) +
464 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400465 if (!info)
466 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700467
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700468 init_completion(&info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700469
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800470 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700471
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800472 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
473 msg->child_relid = channel->offermsg.child_relid;
474 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700475
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800476 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800477 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800478 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800479 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc6977672011-01-26 12:12:08 -0800480 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700481 sizeof(struct vmbus_channel_gpadl_teardown));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700482
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700483 if (ret)
484 goto post_msg_err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700485
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700486 wait_for_completion(&info->waitevent);
487
488post_msg_err:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800489 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800490 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800491 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700492
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700493 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700494 return ret;
495}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700496EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700497
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700498static void reset_channel_cb(void *arg)
499{
500 struct vmbus_channel *channel = arg;
501
502 channel->onchannel_callback = NULL;
503}
504
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700505static int vmbus_close_internal(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700506{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700507 struct vmbus_channel_close_channel *msg;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700508 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700509
Dexuan Cui63d55b22015-12-14 16:01:47 -0800510 /*
511 * process_chn_event(), running in the tasklet, can race
512 * with vmbus_close_internal() in the case of SMP guest, e.g., when
513 * the former is accessing channel->inbound.ring_buffer, the latter
514 * could be freeing the ring_buffer pages.
515 *
516 * To resolve the race, we can serialize them by disabling the
517 * tasklet when the latter is running here.
518 */
Dexuan Cui638fea32016-06-09 18:47:24 -0700519 hv_event_tasklet_disable(channel);
Dexuan Cui63d55b22015-12-14 16:01:47 -0800520
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800521 /*
522 * In case a device driver's probe() fails (e.g.,
523 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
524 * rescinded later (e.g., we dynamically disble an Integrated Service
525 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
526 * here we should skip most of the below cleanup work.
527 */
528 if (channel->state != CHANNEL_OPENED_STATE) {
529 ret = -EINVAL;
530 goto out;
531 }
532
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700533 channel->state = CHANNEL_OPEN_STATE;
534 channel->sc_creation_callback = NULL;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400535 /* Stop callback and cancel the timer asap */
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700536 if (channel->target_cpu != get_cpu()) {
537 put_cpu();
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700538 smp_call_function_single(channel->target_cpu, reset_channel_cb,
539 channel, true);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700540 } else {
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700541 reset_channel_cb(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700542 put_cpu();
543 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700544
Bill Pemberton454f18a2009-07-27 16:47:24 -0400545 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700546
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700547 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700548
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800549 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
550 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700551
Haiyang Zhangc6977672011-01-26 12:12:08 -0800552 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700553
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700554 if (ret) {
555 pr_err("Close failed: close post msg return is %d\n", ret);
556 /*
557 * If we failed to post the close msg,
558 * it is perhaps better to leak memory.
559 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800560 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700561 }
562
Bill Pemberton454f18a2009-07-27 16:47:24 -0400563 /* Tear down the gpadl for the channel's ring buffer */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700564 if (channel->ringbuffer_gpadlhandle) {
565 ret = vmbus_teardown_gpadl(channel,
566 channel->ringbuffer_gpadlhandle);
567 if (ret) {
568 pr_err("Close failed: teardown gpadl return %d\n", ret);
569 /*
570 * If we failed to teardown gpadl,
571 * it is perhaps better to leak memory.
572 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800573 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700574 }
575 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700576
Bill Pemberton454f18a2009-07-27 16:47:24 -0400577 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700578 hv_ringbuffer_cleanup(&channel->outbound);
579 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700580
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800581 free_pages((unsigned long)channel->ringbuffer_pages,
582 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700583
Dexuan Cui63d55b22015-12-14 16:01:47 -0800584out:
Dexuan Cui638fea32016-06-09 18:47:24 -0700585 hv_event_tasklet_enable(channel);
Dexuan Cui63d55b22015-12-14 16:01:47 -0800586
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700587 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700588}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700589
590/*
591 * vmbus_close - Close the specified channel
592 */
593void vmbus_close(struct vmbus_channel *channel)
594{
595 struct list_head *cur, *tmp;
596 struct vmbus_channel *cur_channel;
597
598 if (channel->primary_channel != NULL) {
599 /*
600 * We will only close sub-channels when
601 * the primary is closed.
602 */
603 return;
604 }
605 /*
606 * Close all the sub-channels first and then close the
607 * primary channel.
608 */
609 list_for_each_safe(cur, tmp, &channel->sc_list) {
610 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
611 if (cur_channel->state != CHANNEL_OPENED_STATE)
612 continue;
613 vmbus_close_internal(cur_channel);
614 }
615 /*
616 * Now close the primary.
617 */
618 vmbus_close_internal(channel);
619}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700620EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700621
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800622int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700623 u32 bufferlen, u64 requestid,
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800624 enum vmbus_packet_type type, u32 flags, bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700625{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700626 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700627 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100628 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800629 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700630 u64 aligned_data = 0;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700631 int ret;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800632 bool signal = false;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800633 bool lock = channel->acquire_ring_lock;
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700634 int num_vecs = ((bufferlen != 0) ? 3 : 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700635
Hank Janssen3e7ee492009-07-13 16:02:34 -0700636
Bill Pemberton454f18a2009-07-27 16:47:24 -0400637 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800638 desc.type = type; /* VmbusPacketTypeDataInBand; */
639 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700640 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800641 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
642 desc.len8 = (u16)(packetlen_aligned >> 3);
643 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700644
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800645 bufferlist[0].iov_base = &desc;
646 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
647 bufferlist[1].iov_base = buffer;
648 bufferlist[1].iov_len = bufferlen;
649 bufferlist[2].iov_base = &aligned_data;
650 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700651
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700652 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, num_vecs,
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800653 &signal, lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700654
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700655 /*
656 * Signalling the host is conditional on many factors:
657 * 1. The ring state changed from being empty to non-empty.
658 * This is tracked by the variable "signal".
659 * 2. The variable kick_q tracks if more data will be placed
660 * on the ring. We will not signal if more data is
661 * to be placed.
662 *
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800663 * Based on the channel signal state, we will decide
664 * which signaling policy will be applied.
665 *
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700666 * If we cannot write to the ring-buffer; signal the host
667 * even if we may not have written anything. This is a rare
668 * enough condition that it should not matter.
Dexuan Cui5f363bc2016-01-27 22:29:39 -0800669 * NOTE: in this case, the hvsock channel is an exception, because
670 * it looks the host side's hvsock implementation has a throttling
671 * mechanism which can hurt the performance otherwise.
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700672 */
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800673
674 if (channel->signal_policy)
675 signal = true;
676 else
677 kick_q = true;
678
Dexuan Cui5f363bc2016-01-27 22:29:39 -0800679 if (((ret == 0) && kick_q && signal) ||
680 (ret && !is_hvsock_channel(channel)))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700681 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700682
Hank Janssen3e7ee492009-07-13 16:02:34 -0700683 return ret;
684}
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800685EXPORT_SYMBOL(vmbus_sendpacket_ctl);
686
687/**
688 * vmbus_sendpacket() - Send the specified buffer on the given channel
689 * @channel: Pointer to vmbus_channel structure.
690 * @buffer: Pointer to the buffer you want to receive the data into.
691 * @bufferlen: Maximum size of what the the buffer will hold
692 * @requestid: Identifier of the request
693 * @type: Type of packet that is being send e.g. negotiate, time
694 * packet etc.
695 *
696 * Sends data in @buffer directly to hyper-v via the vmbus
697 * This will send the data unparsed to hyper-v.
698 *
699 * Mainly used by Hyper-V drivers.
700 */
701int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
702 u32 bufferlen, u64 requestid,
703 enum vmbus_packet_type type, u32 flags)
704{
705 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
706 type, flags, true);
707}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700708EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700709
Hank Janssen3e189512010-03-04 22:11:00 +0000710/*
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800711 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
712 * packets using a GPADL Direct packet type. This interface allows you
713 * to control notifying the host. This will be useful for sending
714 * batched data. Also the sender can control the send flags
715 * explicitly.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700716 */
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800717int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700718 struct hv_page_buffer pagebuffers[],
719 u32 pagecount, void *buffer, u32 bufferlen,
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800720 u64 requestid,
721 u32 flags,
722 bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700723{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700724 int ret;
725 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000726 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700727 u32 descsize;
728 u32 packetlen;
729 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800730 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700731 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800732 bool signal = false;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800733 bool lock = channel->acquire_ring_lock;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700734
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700735 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400736 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700737
Hank Janssen3e7ee492009-07-13 16:02:34 -0700738
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700739 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000740 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700741 * largest size we support
742 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700743 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
744 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700745 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700746 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100747 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700748
Bill Pemberton454f18a2009-07-27 16:47:24 -0400749 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800750 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800751 desc.flags = flags;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700752 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
753 desc.length8 = (u16)(packetlen_aligned >> 3);
754 desc.transactionid = requestid;
755 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700756
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700757 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800758 desc.range[i].len = pagebuffers[i].len;
759 desc.range[i].offset = pagebuffers[i].offset;
760 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700761 }
762
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800763 bufferlist[0].iov_base = &desc;
764 bufferlist[0].iov_len = descsize;
765 bufferlist[1].iov_base = buffer;
766 bufferlist[1].iov_len = bufferlen;
767 bufferlist[2].iov_base = &aligned_data;
768 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700769
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800770 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
771 &signal, lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700772
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700773 /*
774 * Signalling the host is conditional on many factors:
775 * 1. The ring state changed from being empty to non-empty.
776 * This is tracked by the variable "signal".
777 * 2. The variable kick_q tracks if more data will be placed
778 * on the ring. We will not signal if more data is
779 * to be placed.
780 *
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800781 * Based on the channel signal state, we will decide
782 * which signaling policy will be applied.
783 *
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700784 * If we cannot write to the ring-buffer; signal the host
785 * even if we may not have written anything. This is a rare
786 * enough condition that it should not matter.
787 */
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800788
789 if (channel->signal_policy)
790 signal = true;
791 else
792 kick_q = true;
793
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700794 if (((ret == 0) && kick_q && signal) || (ret))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700795 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700796
Hank Janssen3e7ee492009-07-13 16:02:34 -0700797 return ret;
798}
K. Y. Srinivasanb3a19b32015-03-18 12:29:29 -0700799EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800800
801/*
802 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
803 * packets using a GPADL Direct packet type.
804 */
805int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
806 struct hv_page_buffer pagebuffers[],
807 u32 pagecount, void *buffer, u32 bufferlen,
808 u64 requestid)
809{
810 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
811 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
812 buffer, bufferlen, requestid,
813 flags, true);
814
815}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700816EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700817
Hank Janssen3e189512010-03-04 22:11:00 +0000818/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700819 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000820 * using a GPADL Direct packet type.
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800821 * The buffer includes the vmbus descriptor.
822 */
823int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
824 struct vmbus_packet_mpb_array *desc,
825 u32 desc_size,
826 void *buffer, u32 bufferlen, u64 requestid)
827{
828 int ret;
829 u32 packetlen;
830 u32 packetlen_aligned;
831 struct kvec bufferlist[3];
832 u64 aligned_data = 0;
833 bool signal = false;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800834 bool lock = channel->acquire_ring_lock;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800835
836 packetlen = desc_size + bufferlen;
837 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
838
839 /* Setup the descriptor */
840 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
841 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
842 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
843 desc->length8 = (u16)(packetlen_aligned >> 3);
844 desc->transactionid = requestid;
845 desc->rangecount = 1;
846
847 bufferlist[0].iov_base = desc;
848 bufferlist[0].iov_len = desc_size;
849 bufferlist[1].iov_base = buffer;
850 bufferlist[1].iov_len = bufferlen;
851 bufferlist[2].iov_base = &aligned_data;
852 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
853
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800854 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
855 &signal, lock);
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800856
857 if (ret == 0 && signal)
858 vmbus_setevent(channel);
859
860 return ret;
861}
862EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
863
864/*
865 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
866 * using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700867 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700868int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700869 struct hv_multipage_buffer *multi_pagebuffer,
870 void *buffer, u32 bufferlen, u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700871{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700872 int ret;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000873 struct vmbus_channel_packet_multipage_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700874 u32 descsize;
875 u32 packetlen;
876 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800877 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700878 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800879 bool signal = false;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800880 bool lock = channel->acquire_ring_lock;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800881 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
882 multi_pagebuffer->len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700883
Tobias Klauser24b8a402014-04-25 17:03:50 +0200884 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400885 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700886
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700887 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000888 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700889 * the largest size we support
890 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700891 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
892 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700893 sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700894 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100895 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700896
Hank Janssen3e7ee492009-07-13 16:02:34 -0700897
Bill Pemberton454f18a2009-07-27 16:47:24 -0400898 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800899 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000900 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700901 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
902 desc.length8 = (u16)(packetlen_aligned >> 3);
903 desc.transactionid = requestid;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000904 desc.rangecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700905
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800906 desc.range.len = multi_pagebuffer->len;
907 desc.range.offset = multi_pagebuffer->offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700908
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800909 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700910 pfncount * sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700911
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800912 bufferlist[0].iov_base = &desc;
913 bufferlist[0].iov_len = descsize;
914 bufferlist[1].iov_base = buffer;
915 bufferlist[1].iov_len = bufferlen;
916 bufferlist[2].iov_base = &aligned_data;
917 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700918
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800919 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
920 &signal, lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700921
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800922 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700923 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700924
Hank Janssen3e7ee492009-07-13 16:02:34 -0700925 return ret;
926}
Greg Kroah-Hartman4cb106f2010-10-21 09:23:59 -0700927EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700928
929/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700930 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700931 * @channel: Pointer to vmbus_channel structure.
932 * @buffer: Pointer to the buffer you want to receive the data into.
933 * @bufferlen: Maximum size of what the the buffer will hold
934 * @buffer_actual_len: The actual size of the data after it was received
935 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700936 *
937 * Receives directly from the hyper-v vmbus and puts the data it received
938 * into Buffer. This will receive the data unparsed from hyper-v.
939 *
940 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700941 */
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800942static inline int
943__vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
944 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
945 bool raw)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700946{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700947 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800948 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700949
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800950 ret = hv_ringbuffer_read(&channel->inbound, buffer, bufferlen,
951 buffer_actual_len, requestid, &signal, raw);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700952
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800953 if (signal)
954 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700955
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800956 return ret;
957}
958
959int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
960 u32 bufferlen, u32 *buffer_actual_len,
961 u64 *requestid)
962{
963 return __vmbus_recvpacket(channel, buffer, bufferlen,
964 buffer_actual_len, requestid, false);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700965}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700966EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700967
Hank Janssen3e189512010-03-04 22:11:00 +0000968/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700969 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700970 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700971int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700972 u32 bufferlen, u32 *buffer_actual_len,
973 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700974{
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800975 return __vmbus_recvpacket(channel, buffer, bufferlen,
976 buffer_actual_len, requestid, true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700977}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700978EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);