blob: 15e833004fc36465de5a5b3df1596ad5830d65d2 [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
K. Y. Srinivasan37242872016-07-01 16:26:37 -070046 /*
47 * For channels marked as in "low latency" mode
48 * bypass the monitor page mechanism.
49 */
50 if ((channel->offermsg.monitor_allocated) &&
51 (!channel->low_latency)) {
Bill Pemberton454f18a2009-07-27 16:47:24 -040052 /* Each u32 represents 32 channels */
Olaf Hering22356582011-03-21 14:41:37 +010053 sync_set_bit(channel->offermsg.child_relid & 31,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080054 (unsigned long *) vmbus_connection.send_int_page +
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080055 (channel->offermsg.child_relid >> 5));
Hank Janssen3e7ee492009-07-13 16:02:34 -070056
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -070057 /* Get the child to parent monitor page */
58 monitorpage = vmbus_connection.monitor_pages[1];
Hank Janssen3e7ee492009-07-13 16:02:34 -070059
Olaf Hering22356582011-03-21 14:41:37 +010060 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080061 (unsigned long *)&monitorpage->trigger_group
62 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -040063
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070064 } else {
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -080065 vmbus_set_event(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -070066 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070067}
68
Hank Janssen3e189512010-03-04 22:11:00 +000069/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070070 * vmbus_open - Open the specified channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070071 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070072int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -070073 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
74 void (*onchannelcallback)(void *context), void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -070075{
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -070076 struct vmbus_channel_open_channel *open_msg;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -070077 struct vmbus_channel_msginfo *open_info = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -070078 void *in, *out;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070079 unsigned long flags;
Nicholas Mc Guire08a95132015-02-27 11:26:01 -080080 int ret, err = 0;
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070081 struct page *page;
Hank Janssen3e7ee492009-07-13 16:02:34 -070082
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -070083 if (send_ringbuffer_size % PAGE_SIZE ||
84 recv_ringbuffer_size % PAGE_SIZE)
85 return -EINVAL;
86
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010087 spin_lock_irqsave(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070088 if (newchannel->state == CHANNEL_OPEN_STATE) {
89 newchannel->state = CHANNEL_OPENING_STATE;
90 } else {
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010091 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070092 return -EINVAL;
93 }
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010094 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070095
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080096 newchannel->onchannel_callback = onchannelcallback;
97 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070098
Bill Pemberton454f18a2009-07-27 16:47:24 -040099 /* Allocate the ring buffer */
K. Y. Srinivasan294409d2015-05-31 21:27:03 -0700100 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
101 GFP_KERNEL|__GFP_ZERO,
102 get_order(send_ringbuffer_size +
103 recv_ringbuffer_size));
104
105 if (!page)
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700106 page = alloc_pages(GFP_KERNEL|__GFP_ZERO,
107 get_order(send_ringbuffer_size +
108 recv_ringbuffer_size));
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800109
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700110 if (!page) {
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800111 err = -ENOMEM;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700112 goto error_set_chnstate;
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800113 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700114
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700115 out = page_address(page);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700116 in = (void *)((unsigned long)out + send_ringbuffer_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700117
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800118 newchannel->ringbuffer_pages = out;
119 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700120 recv_ringbuffer_size) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700121
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700122 ret = hv_ringbuffer_init(
123 &newchannel->outbound, out, send_ringbuffer_size);
124
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000125 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400126 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700127 goto error_free_pages;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400128 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700129
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700130 ret = hv_ringbuffer_init(
131 &newchannel->inbound, in, recv_ringbuffer_size);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000132 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400133 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700134 goto error_free_pages;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400135 }
136
Hank Janssen3e7ee492009-07-13 16:02:34 -0700137
Bill Pemberton454f18a2009-07-27 16:47:24 -0400138 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800139 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700140
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700141 ret = vmbus_establish_gpadl(newchannel,
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800142 newchannel->outbound.ring_buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700143 send_ringbuffer_size +
144 recv_ringbuffer_size,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800145 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400146
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000147 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400148 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700149 goto error_free_pages;
Bill Pembertonb94ef342010-05-05 15:27:37 -0400150 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700151
Bill Pemberton454f18a2009-07-27 16:47:24 -0400152 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700153 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700154 sizeof(struct vmbus_channel_open_channel),
155 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700156 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400157 err = -ENOMEM;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700158 goto error_free_gpadl;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400159 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700160
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700161 init_completion(&open_info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700162
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700163 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700164 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
165 open_msg->openid = newchannel->offermsg.child_relid;
166 open_msg->child_relid = newchannel->offermsg.child_relid;
167 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
168 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700169 PAGE_SHIFT;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800170 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700171
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700172 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400173 err = -EINVAL;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700174 goto error_free_gpadl;
Bill Pembertonc827f942010-05-05 15:27:38 -0400175 }
176
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700177 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700178 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700179
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800180 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700181 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800182 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800183 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700184
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700185 ret = vmbus_post_msg(open_msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700186 sizeof(struct vmbus_channel_open_channel));
Hank Janssen98e08702011-03-29 13:58:44 -0700187
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700188 if (ret != 0) {
189 err = ret;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700190 goto error_clean_msglist;
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700191 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700192
Vitaly Kuznetsov396e2872016-06-09 17:08:56 -0700193 wait_for_completion(&open_info->waitevent);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800194
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800195 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700196 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800197 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700198
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700199 if (open_info->response.open_result.status) {
200 err = -EAGAIN;
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700201 goto error_free_gpadl;
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700202 }
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700203
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700204 newchannel->state = CHANNEL_OPENED_STATE;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700205 kfree(open_info);
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700206 return 0;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400207
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700208error_clean_msglist:
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700209 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
210 list_del(&open_info->msglistentry);
211 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
212
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700213error_free_gpadl:
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800214 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700215 kfree(open_info);
216error_free_pages:
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800217 free_pages((unsigned long)out,
218 get_order(send_ringbuffer_size + recv_ringbuffer_size));
Vitaly Kuznetsov98f531b2016-09-02 05:58:19 -0700219error_set_chnstate:
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800220 newchannel->state = CHANNEL_OPEN_STATE;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400221 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700222}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700223EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700224
Dexuan Cui5c23a1a2016-01-27 22:29:40 -0800225/* Used for Hyper-V Socket: a guest client's connect() to the host */
226int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
227 const uuid_le *shv_host_servie_id)
228{
229 struct vmbus_channel_tl_connect_request conn_msg;
230
231 memset(&conn_msg, 0, sizeof(conn_msg));
232 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
233 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
234 conn_msg.host_service_id = *shv_host_servie_id;
235
236 return vmbus_post_msg(&conn_msg, sizeof(conn_msg));
237}
238EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
239
Hank Janssen3e189512010-03-04 22:11:00 +0000240/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700241 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700242 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700243static int create_gpadl_header(void *kbuffer, u32 size,
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700244 struct vmbus_channel_msginfo **msginfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700245{
246 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700247 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700248 struct vmbus_channel_gpadl_header *gpadl_header;
249 struct vmbus_channel_gpadl_body *gpadl_body;
250 struct vmbus_channel_msginfo *msgheader;
251 struct vmbus_channel_msginfo *msgbody = NULL;
252 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700253
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700254 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700255
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700256 pagecount = size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700257
Bill Pemberton454f18a2009-07-27 16:47:24 -0400258 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700259 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700260 sizeof(struct vmbus_channel_gpadl_header) -
261 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700262 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700263
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700264 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700265 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400266 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700267 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700268 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700269 sizeof(struct gpa_range) + pfncount * sizeof(u64);
270 msgheader = kzalloc(msgsize, GFP_KERNEL);
271 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400272 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700273
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800274 INIT_LIST_HEAD(&msgheader->submsglist);
275 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700276
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700277 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800278 msgheader->msg;
279 gpadl_header->rangecount = 1;
280 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700281 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800282 gpadl_header->range[0].byte_offset = 0;
283 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700284 for (i = 0; i < pfncount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800285 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
286 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700287 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700288
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700289 pfnsum = pfncount;
290 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700291
Bill Pemberton454f18a2009-07-27 16:47:24 -0400292 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700293 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700294 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700295 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700296
Bill Pemberton454f18a2009-07-27 16:47:24 -0400297 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700298 while (pfnleft) {
299 if (pfnleft > pfncount)
300 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700301 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700302 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700303
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700304 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700305 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700306 pfncurr * sizeof(u64);
307 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700308
309 if (!msgbody) {
310 struct vmbus_channel_msginfo *pos = NULL;
311 struct vmbus_channel_msginfo *tmp = NULL;
312 /*
313 * Free up all the allocated messages.
314 */
315 list_for_each_entry_safe(pos, tmp,
316 &msgheader->submsglist,
317 msglistentry) {
318
319 list_del(&pos->msglistentry);
320 kfree(pos);
321 }
322
Bill Pembertond1c250b2010-05-05 15:27:35 -0400323 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700324 }
325
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800326 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700327 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800328 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700329
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700330 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700331 * Gpadl is u32 and we are using a pointer which could
332 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700333 * This is governed by the guest/host protocol and
334 * so the hypervisor gurantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700335 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700336 for (i = 0; i < pfncurr; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800337 gpadl_body->pfn[i] = slow_virt_to_phys(
338 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
339 PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700340
Bill Pemberton454f18a2009-07-27 16:47:24 -0400341 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800342 list_add_tail(&msgbody->msglistentry,
343 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700344 pfnsum += pfncurr;
345 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700346 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700347 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400348 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700349 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700350 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700351 sizeof(struct gpa_range) + pagecount * sizeof(u64);
352 msgheader = kzalloc(msgsize, GFP_KERNEL);
353 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400354 goto nomem;
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700355
356 INIT_LIST_HEAD(&msgheader->submsglist);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800357 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700358
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700359 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800360 msgheader->msg;
361 gpadl_header->rangecount = 1;
362 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700363 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800364 gpadl_header->range[0].byte_offset = 0;
365 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700366 for (i = 0; i < pagecount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800367 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
368 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700369
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700370 *msginfo = msgheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700371 }
372
373 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400374nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700375 kfree(msgheader);
376 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400377 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700378}
379
Hank Janssen3e189512010-03-04 22:11:00 +0000380/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700381 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700382 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700383 * @channel: a channel
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800384 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700385 * @size: page-size multiple
386 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700387 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700388int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700389 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700390{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700391 struct vmbus_channel_gpadl_header *gpadlmsg;
392 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700393 struct vmbus_channel_msginfo *msginfo = NULL;
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700394 struct vmbus_channel_msginfo *submsginfo, *tmp;
Bill Pemberton53af5452009-09-11 21:46:44 -0400395 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700396 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700397 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400398 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700399
K. Y. Srinivasan9f52a162015-01-09 23:54:33 -0800400 next_gpadl_handle =
401 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700402
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700403 ret = create_gpadl_header(kbuffer, size, &msginfo);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400404 if (ret)
405 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700406
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700407 init_completion(&msginfo->waitevent);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400408
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800409 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
410 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
411 gpadlmsg->child_relid = channel->offermsg.child_relid;
412 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700413
Hank Janssen3e7ee492009-07-13 16:02:34 -0700414
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800415 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800416 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800417 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700418
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800419 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700420
Haiyang Zhangc6977672011-01-26 12:12:08 -0800421 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700422 sizeof(*msginfo));
Hank Janssen98e08702011-03-29 13:58:44 -0700423 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700424 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700425
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700426 list_for_each(curr, &msginfo->submsglist) {
427 submsginfo = (struct vmbus_channel_msginfo *)curr;
428 gpadl_body =
429 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Bill Pemberton53af5452009-09-11 21:46:44 -0400430
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700431 gpadl_body->header.msgtype =
432 CHANNELMSG_GPADL_BODY;
433 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700434
Vitaly Kuznetsov4d637632016-06-03 17:09:23 -0700435 ret = vmbus_post_msg(gpadl_body,
436 submsginfo->msgsize -
437 sizeof(*submsginfo));
438 if (ret != 0)
439 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700440
Hank Janssen3e7ee492009-07-13 16:02:34 -0700441 }
K. Y. Srinivasan72c6b712014-08-27 16:25:34 -0700442 wait_for_completion(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700443
Bill Pemberton454f18a2009-07-27 16:47:24 -0400444 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800445 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700446
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700447cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800448 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800449 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800450 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Vitaly Kuznetsov7cc80c92016-06-03 17:09:24 -0700451 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
452 msglistentry) {
453 kfree(submsginfo);
454 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700455
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700456 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700457 return ret;
458}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700459EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700460
Hank Janssen3e189512010-03-04 22:11:00 +0000461/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700462 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700463 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700464int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700465{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700466 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700467 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700468 unsigned long flags;
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700469 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700470
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700471 info = kmalloc(sizeof(*info) +
472 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400473 if (!info)
474 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700475
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700476 init_completion(&info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700477
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800478 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700479
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800480 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
481 msg->child_relid = channel->offermsg.child_relid;
482 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700483
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800484 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800485 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800486 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800487 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc6977672011-01-26 12:12:08 -0800488 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700489 sizeof(struct vmbus_channel_gpadl_teardown));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700490
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700491 if (ret)
492 goto post_msg_err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700493
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700494 wait_for_completion(&info->waitevent);
495
496post_msg_err:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800497 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800498 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800499 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700500
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700501 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700502 return ret;
503}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700504EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700505
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700506static void reset_channel_cb(void *arg)
507{
508 struct vmbus_channel *channel = arg;
509
510 channel->onchannel_callback = NULL;
511}
512
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700513static int vmbus_close_internal(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700514{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700515 struct vmbus_channel_close_channel *msg;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700516 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700517
Dexuan Cui63d55b22015-12-14 16:01:47 -0800518 /*
519 * process_chn_event(), running in the tasklet, can race
520 * with vmbus_close_internal() in the case of SMP guest, e.g., when
521 * the former is accessing channel->inbound.ring_buffer, the latter
522 * could be freeing the ring_buffer pages.
523 *
524 * To resolve the race, we can serialize them by disabling the
525 * tasklet when the latter is running here.
526 */
Dexuan Cui638fea32016-06-09 18:47:24 -0700527 hv_event_tasklet_disable(channel);
Dexuan Cui63d55b22015-12-14 16:01:47 -0800528
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800529 /*
530 * In case a device driver's probe() fails (e.g.,
531 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
532 * rescinded later (e.g., we dynamically disble an Integrated Service
533 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
534 * here we should skip most of the below cleanup work.
535 */
536 if (channel->state != CHANNEL_OPENED_STATE) {
537 ret = -EINVAL;
538 goto out;
539 }
540
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700541 channel->state = CHANNEL_OPEN_STATE;
542 channel->sc_creation_callback = NULL;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400543 /* Stop callback and cancel the timer asap */
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700544 if (channel->target_cpu != get_cpu()) {
545 put_cpu();
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700546 smp_call_function_single(channel->target_cpu, reset_channel_cb,
547 channel, true);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700548 } else {
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700549 reset_channel_cb(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700550 put_cpu();
551 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700552
Bill Pemberton454f18a2009-07-27 16:47:24 -0400553 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700554
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700555 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700556
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800557 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
558 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700559
Haiyang Zhangc6977672011-01-26 12:12:08 -0800560 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700561
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700562 if (ret) {
563 pr_err("Close failed: close post msg return is %d\n", ret);
564 /*
565 * If we failed to post the close msg,
566 * it is perhaps better to leak memory.
567 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800568 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700569 }
570
Bill Pemberton454f18a2009-07-27 16:47:24 -0400571 /* Tear down the gpadl for the channel's ring buffer */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700572 if (channel->ringbuffer_gpadlhandle) {
573 ret = vmbus_teardown_gpadl(channel,
574 channel->ringbuffer_gpadlhandle);
575 if (ret) {
576 pr_err("Close failed: teardown gpadl return %d\n", ret);
577 /*
578 * If we failed to teardown gpadl,
579 * it is perhaps better to leak memory.
580 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800581 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700582 }
583 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700584
Bill Pemberton454f18a2009-07-27 16:47:24 -0400585 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700586 hv_ringbuffer_cleanup(&channel->outbound);
587 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700588
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800589 free_pages((unsigned long)channel->ringbuffer_pages,
590 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700591
Dexuan Cui63d55b22015-12-14 16:01:47 -0800592out:
Dexuan Cui638fea32016-06-09 18:47:24 -0700593 hv_event_tasklet_enable(channel);
Dexuan Cui63d55b22015-12-14 16:01:47 -0800594
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700595 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700596}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700597
598/*
599 * vmbus_close - Close the specified channel
600 */
601void vmbus_close(struct vmbus_channel *channel)
602{
603 struct list_head *cur, *tmp;
604 struct vmbus_channel *cur_channel;
605
606 if (channel->primary_channel != NULL) {
607 /*
608 * We will only close sub-channels when
609 * the primary is closed.
610 */
611 return;
612 }
613 /*
614 * Close all the sub-channels first and then close the
615 * primary channel.
616 */
617 list_for_each_safe(cur, tmp, &channel->sc_list) {
618 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
619 if (cur_channel->state != CHANNEL_OPENED_STATE)
620 continue;
621 vmbus_close_internal(cur_channel);
622 }
623 /*
624 * Now close the primary.
625 */
626 vmbus_close_internal(channel);
627}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700628EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700629
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800630int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700631 u32 bufferlen, u64 requestid,
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800632 enum vmbus_packet_type type, u32 flags, bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700633{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700634 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700635 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100636 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800637 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700638 u64 aligned_data = 0;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700639 int ret;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800640 bool signal = false;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800641 bool lock = channel->acquire_ring_lock;
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700642 int num_vecs = ((bufferlen != 0) ? 3 : 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700643
Hank Janssen3e7ee492009-07-13 16:02:34 -0700644
Bill Pemberton454f18a2009-07-27 16:47:24 -0400645 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800646 desc.type = type; /* VmbusPacketTypeDataInBand; */
647 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700648 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800649 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
650 desc.len8 = (u16)(packetlen_aligned >> 3);
651 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700652
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800653 bufferlist[0].iov_base = &desc;
654 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
655 bufferlist[1].iov_base = buffer;
656 bufferlist[1].iov_len = bufferlen;
657 bufferlist[2].iov_base = &aligned_data;
658 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700659
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700660 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, num_vecs,
K. Y. Srinivasanccef9bc2016-07-01 16:26:35 -0700661 &signal, lock, channel->signal_policy);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700662
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700663 /*
664 * Signalling the host is conditional on many factors:
665 * 1. The ring state changed from being empty to non-empty.
666 * This is tracked by the variable "signal".
667 * 2. The variable kick_q tracks if more data will be placed
668 * on the ring. We will not signal if more data is
669 * to be placed.
670 *
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800671 * Based on the channel signal state, we will decide
672 * which signaling policy will be applied.
673 *
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700674 * If we cannot write to the ring-buffer; signal the host
675 * even if we may not have written anything. This is a rare
676 * enough condition that it should not matter.
Dexuan Cui5f363bc2016-01-27 22:29:39 -0800677 * NOTE: in this case, the hvsock channel is an exception, because
678 * it looks the host side's hvsock implementation has a throttling
679 * mechanism which can hurt the performance otherwise.
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700680 */
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800681
Dexuan Cui5f363bc2016-01-27 22:29:39 -0800682 if (((ret == 0) && kick_q && signal) ||
683 (ret && !is_hvsock_channel(channel)))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700684 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700685
Hank Janssen3e7ee492009-07-13 16:02:34 -0700686 return ret;
687}
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800688EXPORT_SYMBOL(vmbus_sendpacket_ctl);
689
690/**
691 * vmbus_sendpacket() - Send the specified buffer on the given channel
692 * @channel: Pointer to vmbus_channel structure.
693 * @buffer: Pointer to the buffer you want to receive the data into.
694 * @bufferlen: Maximum size of what the the buffer will hold
695 * @requestid: Identifier of the request
696 * @type: Type of packet that is being send e.g. negotiate, time
697 * packet etc.
698 *
699 * Sends data in @buffer directly to hyper-v via the vmbus
700 * This will send the data unparsed to hyper-v.
701 *
702 * Mainly used by Hyper-V drivers.
703 */
704int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
705 u32 bufferlen, u64 requestid,
706 enum vmbus_packet_type type, u32 flags)
707{
708 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
709 type, flags, true);
710}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700711EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700712
Hank Janssen3e189512010-03-04 22:11:00 +0000713/*
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800714 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
715 * packets using a GPADL Direct packet type. This interface allows you
716 * to control notifying the host. This will be useful for sending
717 * batched data. Also the sender can control the send flags
718 * explicitly.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700719 */
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800720int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700721 struct hv_page_buffer pagebuffers[],
722 u32 pagecount, void *buffer, u32 bufferlen,
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800723 u64 requestid,
724 u32 flags,
725 bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700726{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700727 int ret;
728 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000729 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700730 u32 descsize;
731 u32 packetlen;
732 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800733 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700734 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800735 bool signal = false;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800736 bool lock = channel->acquire_ring_lock;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700737
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700738 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400739 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700740
Hank Janssen3e7ee492009-07-13 16:02:34 -0700741
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700742 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000743 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700744 * largest size we support
745 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700746 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
747 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700748 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700749 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100750 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700751
Bill Pemberton454f18a2009-07-27 16:47:24 -0400752 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800753 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800754 desc.flags = flags;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700755 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
756 desc.length8 = (u16)(packetlen_aligned >> 3);
757 desc.transactionid = requestid;
758 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700759
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700760 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800761 desc.range[i].len = pagebuffers[i].len;
762 desc.range[i].offset = pagebuffers[i].offset;
763 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700764 }
765
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800766 bufferlist[0].iov_base = &desc;
767 bufferlist[0].iov_len = descsize;
768 bufferlist[1].iov_base = buffer;
769 bufferlist[1].iov_len = bufferlen;
770 bufferlist[2].iov_base = &aligned_data;
771 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700772
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800773 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
K. Y. Srinivasanccef9bc2016-07-01 16:26:35 -0700774 &signal, lock, channel->signal_policy);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700775
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700776 /*
777 * Signalling the host is conditional on many factors:
778 * 1. The ring state changed from being empty to non-empty.
779 * This is tracked by the variable "signal".
780 * 2. The variable kick_q tracks if more data will be placed
781 * on the ring. We will not signal if more data is
782 * to be placed.
783 *
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800784 * Based on the channel signal state, we will decide
785 * which signaling policy will be applied.
786 *
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700787 * If we cannot write to the ring-buffer; signal the host
788 * even if we may not have written anything. This is a rare
789 * enough condition that it should not matter.
790 */
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800791
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700792 if (((ret == 0) && kick_q && signal) || (ret))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700793 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700794
Hank Janssen3e7ee492009-07-13 16:02:34 -0700795 return ret;
796}
K. Y. Srinivasanb3a19b32015-03-18 12:29:29 -0700797EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800798
799/*
800 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
801 * packets using a GPADL Direct packet type.
802 */
803int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
804 struct hv_page_buffer pagebuffers[],
805 u32 pagecount, void *buffer, u32 bufferlen,
806 u64 requestid)
807{
808 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
809 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
810 buffer, bufferlen, requestid,
811 flags, true);
812
813}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700814EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700815
Hank Janssen3e189512010-03-04 22:11:00 +0000816/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700817 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000818 * using a GPADL Direct packet type.
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800819 * The buffer includes the vmbus descriptor.
820 */
821int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
822 struct vmbus_packet_mpb_array *desc,
823 u32 desc_size,
824 void *buffer, u32 bufferlen, u64 requestid)
825{
826 int ret;
827 u32 packetlen;
828 u32 packetlen_aligned;
829 struct kvec bufferlist[3];
830 u64 aligned_data = 0;
831 bool signal = false;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800832 bool lock = channel->acquire_ring_lock;
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800833
834 packetlen = desc_size + bufferlen;
835 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
836
837 /* Setup the descriptor */
838 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
839 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
840 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
841 desc->length8 = (u16)(packetlen_aligned >> 3);
842 desc->transactionid = requestid;
843 desc->rangecount = 1;
844
845 bufferlist[0].iov_base = desc;
846 bufferlist[0].iov_len = desc_size;
847 bufferlist[1].iov_base = buffer;
848 bufferlist[1].iov_len = bufferlen;
849 bufferlist[2].iov_base = &aligned_data;
850 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
851
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800852 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
K. Y. Srinivasanccef9bc2016-07-01 16:26:35 -0700853 &signal, lock, channel->signal_policy);
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800854
855 if (ret == 0 && signal)
856 vmbus_setevent(channel);
857
858 return ret;
859}
860EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
861
862/*
863 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
864 * using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700865 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700866int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700867 struct hv_multipage_buffer *multi_pagebuffer,
868 void *buffer, u32 bufferlen, u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700869{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700870 int ret;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000871 struct vmbus_channel_packet_multipage_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700872 u32 descsize;
873 u32 packetlen;
874 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800875 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700876 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800877 bool signal = false;
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800878 bool lock = channel->acquire_ring_lock;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800879 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
880 multi_pagebuffer->len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700881
Tobias Klauser24b8a402014-04-25 17:03:50 +0200882 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400883 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700884
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700885 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000886 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700887 * the largest size we support
888 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700889 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
890 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700891 sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700892 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100893 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700894
Hank Janssen3e7ee492009-07-13 16:02:34 -0700895
Bill Pemberton454f18a2009-07-27 16:47:24 -0400896 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800897 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000898 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700899 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
900 desc.length8 = (u16)(packetlen_aligned >> 3);
901 desc.transactionid = requestid;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000902 desc.rangecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700903
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800904 desc.range.len = multi_pagebuffer->len;
905 desc.range.offset = multi_pagebuffer->offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700906
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800907 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700908 pfncount * sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700909
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800910 bufferlist[0].iov_base = &desc;
911 bufferlist[0].iov_len = descsize;
912 bufferlist[1].iov_base = buffer;
913 bufferlist[1].iov_len = bufferlen;
914 bufferlist[2].iov_base = &aligned_data;
915 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700916
K. Y. Srinivasanfe760e42016-01-27 22:29:45 -0800917 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
K. Y. Srinivasanccef9bc2016-07-01 16:26:35 -0700918 &signal, lock, channel->signal_policy);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700919
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800920 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700921 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700922
Hank Janssen3e7ee492009-07-13 16:02:34 -0700923 return ret;
924}
Greg Kroah-Hartman4cb106f2010-10-21 09:23:59 -0700925EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700926
927/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700928 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700929 * @channel: Pointer to vmbus_channel structure.
930 * @buffer: Pointer to the buffer you want to receive the data into.
931 * @bufferlen: Maximum size of what the the buffer will hold
932 * @buffer_actual_len: The actual size of the data after it was received
933 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700934 *
935 * Receives directly from the hyper-v vmbus and puts the data it received
936 * into Buffer. This will receive the data unparsed from hyper-v.
937 *
938 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700939 */
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800940static inline int
941__vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
942 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
943 bool raw)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700944{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700945 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800946 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700947
Vitaly Kuznetsov940b68e2015-12-14 19:02:01 -0800948 ret = hv_ringbuffer_read(&channel->inbound, buffer, bufferlen,
949 buffer_actual_len, requestid, &signal, raw);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700950
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800951 if (signal)
952 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700953
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800954 return ret;
955}
956
957int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
958 u32 bufferlen, u32 *buffer_actual_len,
959 u64 *requestid)
960{
961 return __vmbus_recvpacket(channel, buffer, bufferlen,
962 buffer_actual_len, requestid, false);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700963}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700964EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700965
Hank Janssen3e189512010-03-04 22:11:00 +0000966/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700967 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700968 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700969int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700970 u32 bufferlen, u32 *buffer_actual_len,
971 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700972{
Vitaly Kuznetsov667d3742015-12-14 19:02:00 -0800973 return __vmbus_recvpacket(channel, buffer, bufferlen,
974 buffer_actual_len, requestid, true);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700975}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700976EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);