blob: 54da66dc7d16c033db21664606a0fe7efc576535 [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>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070031
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070032#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070033
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080034#define NUM_PAGES_SPANNED(addr, len) \
35((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
36
Hank Janssen3e189512010-03-04 22:11:00 +000037/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070038 * vmbus_setevent- Trigger an event notification on the specified
Hank Janssen3e189512010-03-04 22:11:00 +000039 * channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070040 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070041static void vmbus_setevent(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -070042{
Haiyang Zhang39d70a42010-09-30 10:52:13 -070043 struct hv_monitor_page *monitorpage;
Hank Janssen3e7ee492009-07-13 16:02:34 -070044
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080045 if (channel->offermsg.monitor_allocated) {
Bill Pemberton454f18a2009-07-27 16:47:24 -040046 /* Each u32 represents 32 channels */
Olaf Hering22356582011-03-21 14:41:37 +010047 sync_set_bit(channel->offermsg.child_relid & 31,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080048 (unsigned long *) vmbus_connection.send_int_page +
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080049 (channel->offermsg.child_relid >> 5));
Hank Janssen3e7ee492009-07-13 16:02:34 -070050
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -070051 /* Get the child to parent monitor page */
52 monitorpage = vmbus_connection.monitor_pages[1];
Hank Janssen3e7ee492009-07-13 16:02:34 -070053
Olaf Hering22356582011-03-21 14:41:37 +010054 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080055 (unsigned long *)&monitorpage->trigger_group
56 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -040057
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070058 } else {
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -080059 vmbus_set_event(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -070060 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070061}
62
Hank Janssen3e189512010-03-04 22:11:00 +000063/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070064 * vmbus_open - Open the specified channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070065 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070066int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -070067 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
68 void (*onchannelcallback)(void *context), void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -070069{
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -070070 struct vmbus_channel_open_channel *open_msg;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -070071 struct vmbus_channel_msginfo *open_info = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -070072 void *in, *out;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070073 unsigned long flags;
Nicholas Mc Guire08a95132015-02-27 11:26:01 -080074 int ret, err = 0;
75 unsigned long t;
Hank Janssen3e7ee492009-07-13 16:02:34 -070076
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010077 spin_lock_irqsave(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070078 if (newchannel->state == CHANNEL_OPEN_STATE) {
79 newchannel->state = CHANNEL_OPENING_STATE;
80 } else {
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010081 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070082 return -EINVAL;
83 }
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010084 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070085
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080086 newchannel->onchannel_callback = onchannelcallback;
87 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070088
Bill Pemberton454f18a2009-07-27 16:47:24 -040089 /* Allocate the ring buffer */
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -080090 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
91 get_order(send_ringbuffer_size + recv_ringbuffer_size));
92
Dexuan Cuiac0d12b2015-02-27 11:26:00 -080093 if (!out) {
94 err = -ENOMEM;
95 goto error0;
96 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070097
Haiyang Zhang39d70a42010-09-30 10:52:13 -070098 in = (void *)((unsigned long)out + send_ringbuffer_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -070099
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800100 newchannel->ringbuffer_pages = out;
101 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700102 recv_ringbuffer_size) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700103
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700104 ret = hv_ringbuffer_init(
105 &newchannel->outbound, out, send_ringbuffer_size);
106
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000107 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400108 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700109 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400110 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700111
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700112 ret = hv_ringbuffer_init(
113 &newchannel->inbound, in, recv_ringbuffer_size);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000114 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400115 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700116 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400117 }
118
Hank Janssen3e7ee492009-07-13 16:02:34 -0700119
Bill Pemberton454f18a2009-07-27 16:47:24 -0400120 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800121 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700122
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700123 ret = vmbus_establish_gpadl(newchannel,
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800124 newchannel->outbound.ring_buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700125 send_ringbuffer_size +
126 recv_ringbuffer_size,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800127 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400128
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000129 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400130 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700131 goto error0;
Bill Pembertonb94ef342010-05-05 15:27:37 -0400132 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700133
Bill Pemberton454f18a2009-07-27 16:47:24 -0400134 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700135 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700136 sizeof(struct vmbus_channel_open_channel),
137 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700138 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400139 err = -ENOMEM;
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800140 goto error_gpadl;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400141 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700142
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700143 init_completion(&open_info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700144
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700145 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700146 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
147 open_msg->openid = newchannel->offermsg.child_relid;
148 open_msg->child_relid = newchannel->offermsg.child_relid;
149 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
150 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700151 PAGE_SHIFT;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800152 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700153
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700154 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400155 err = -EINVAL;
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800156 goto error_gpadl;
Bill Pembertonc827f942010-05-05 15:27:38 -0400157 }
158
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700159 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700160 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700161
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800162 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700163 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800164 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800165 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700166
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700167 ret = vmbus_post_msg(open_msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700168 sizeof(struct vmbus_channel_open_channel));
Hank Janssen98e08702011-03-29 13:58:44 -0700169
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700170 if (ret != 0) {
171 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700172 goto error1;
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700173 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700174
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700175 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700176 if (t == 0) {
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800177 err = -ETIMEDOUT;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700178 goto error1;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800179 }
180
Hank Janssen3e7ee492009-07-13 16:02:34 -0700181
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700182 if (open_info->response.open_result.status)
183 err = open_info->response.open_result.status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700184
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800185 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700186 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800187 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700188
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700189 if (err == 0)
190 newchannel->state = CHANNEL_OPENED_STATE;
191
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700192 kfree(open_info);
Hank Janssen98e08702011-03-29 13:58:44 -0700193 return err;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400194
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700195error1:
196 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
197 list_del(&open_info->msglistentry);
198 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
199
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800200error_gpadl:
201 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
202
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700203error0:
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800204 free_pages((unsigned long)out,
205 get_order(send_ringbuffer_size + recv_ringbuffer_size));
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700206 kfree(open_info);
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800207 newchannel->state = CHANNEL_OPEN_STATE;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400208 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700209}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700210EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700211
Hank Janssen3e189512010-03-04 22:11:00 +0000212/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700213 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700214 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700215static int create_gpadl_header(void *kbuffer, u32 size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700216 struct vmbus_channel_msginfo **msginfo,
217 u32 *messagecount)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700218{
219 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700220 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700221 struct vmbus_channel_gpadl_header *gpadl_header;
222 struct vmbus_channel_gpadl_body *gpadl_body;
223 struct vmbus_channel_msginfo *msgheader;
224 struct vmbus_channel_msginfo *msgbody = NULL;
225 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700226
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700227 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700228
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700229 pagecount = size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700230
Bill Pemberton454f18a2009-07-27 16:47:24 -0400231 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700232 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700233 sizeof(struct vmbus_channel_gpadl_header) -
234 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700235 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700236
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700237 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700238 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400239 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700240 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700241 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700242 sizeof(struct gpa_range) + pfncount * sizeof(u64);
243 msgheader = kzalloc(msgsize, GFP_KERNEL);
244 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400245 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700246
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800247 INIT_LIST_HEAD(&msgheader->submsglist);
248 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700249
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700250 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800251 msgheader->msg;
252 gpadl_header->rangecount = 1;
253 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700254 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800255 gpadl_header->range[0].byte_offset = 0;
256 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700257 for (i = 0; i < pfncount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800258 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
259 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700260 *msginfo = msgheader;
261 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700262
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700263 pfnsum = pfncount;
264 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700265
Bill Pemberton454f18a2009-07-27 16:47:24 -0400266 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700267 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700268 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700269 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700270
Bill Pemberton454f18a2009-07-27 16:47:24 -0400271 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700272 while (pfnleft) {
273 if (pfnleft > pfncount)
274 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700275 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700276 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700277
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700278 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700279 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700280 pfncurr * sizeof(u64);
281 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700282
283 if (!msgbody) {
284 struct vmbus_channel_msginfo *pos = NULL;
285 struct vmbus_channel_msginfo *tmp = NULL;
286 /*
287 * Free up all the allocated messages.
288 */
289 list_for_each_entry_safe(pos, tmp,
290 &msgheader->submsglist,
291 msglistentry) {
292
293 list_del(&pos->msglistentry);
294 kfree(pos);
295 }
296
Bill Pembertond1c250b2010-05-05 15:27:35 -0400297 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700298 }
299
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800300 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700301 (*messagecount)++;
302 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800303 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700304
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700305 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700306 * Gpadl is u32 and we are using a pointer which could
307 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700308 * This is governed by the guest/host protocol and
309 * so the hypervisor gurantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700310 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700311 for (i = 0; i < pfncurr; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800312 gpadl_body->pfn[i] = slow_virt_to_phys(
313 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
314 PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700315
Bill Pemberton454f18a2009-07-27 16:47:24 -0400316 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800317 list_add_tail(&msgbody->msglistentry,
318 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700319 pfnsum += pfncurr;
320 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700321 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700322 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400323 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700324 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700325 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700326 sizeof(struct gpa_range) + pagecount * sizeof(u64);
327 msgheader = kzalloc(msgsize, GFP_KERNEL);
328 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400329 goto nomem;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800330 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700331
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700332 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800333 msgheader->msg;
334 gpadl_header->rangecount = 1;
335 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700336 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800337 gpadl_header->range[0].byte_offset = 0;
338 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700339 for (i = 0; i < pagecount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800340 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
341 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700342
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700343 *msginfo = msgheader;
344 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700345 }
346
347 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400348nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700349 kfree(msgheader);
350 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400351 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700352}
353
Hank Janssen3e189512010-03-04 22:11:00 +0000354/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700355 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700356 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700357 * @channel: a channel
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800358 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700359 * @size: page-size multiple
360 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700361 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700362int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700363 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700364{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700365 struct vmbus_channel_gpadl_header *gpadlmsg;
366 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700367 struct vmbus_channel_msginfo *msginfo = NULL;
368 struct vmbus_channel_msginfo *submsginfo;
369 u32 msgcount;
Bill Pemberton53af5452009-09-11 21:46:44 -0400370 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700371 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700372 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400373 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700374
K. Y. Srinivasan9f52a162015-01-09 23:54:33 -0800375 next_gpadl_handle =
376 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700377
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700378 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400379 if (ret)
380 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700381
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700382 init_completion(&msginfo->waitevent);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400383
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800384 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
385 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
386 gpadlmsg->child_relid = channel->offermsg.child_relid;
387 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700388
Hank Janssen3e7ee492009-07-13 16:02:34 -0700389
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800390 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800391 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800392 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700393
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800394 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700395
Haiyang Zhangc6977672011-01-26 12:12:08 -0800396 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700397 sizeof(*msginfo));
Hank Janssen98e08702011-03-29 13:58:44 -0700398 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700399 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700400
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700401 if (msgcount > 1) {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800402 list_for_each(curr, &msginfo->submsglist) {
Bill Pemberton53af5452009-09-11 21:46:44 -0400403
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700404 submsginfo = (struct vmbus_channel_msginfo *)curr;
405 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800406 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700407
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800408 gpadl_body->header.msgtype =
409 CHANNELMSG_GPADL_BODY;
410 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700411
Haiyang Zhangc6977672011-01-26 12:12:08 -0800412 ret = vmbus_post_msg(gpadl_body,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800413 submsginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700414 sizeof(*submsginfo));
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000415 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700416 goto cleanup;
Bill Pemberton99259152010-05-05 15:27:36 -0400417
Hank Janssen3e7ee492009-07-13 16:02:34 -0700418 }
419 }
K. Y. Srinivasan72c6b712014-08-27 16:25:34 -0700420 wait_for_completion(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700421
Bill Pemberton454f18a2009-07-27 16:47:24 -0400422 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800423 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700424
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700425cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800426 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800427 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800428 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700429
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700430 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700431 return ret;
432}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700433EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700434
Hank Janssen3e189512010-03-04 22:11:00 +0000435/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700436 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700437 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700438int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700439{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700440 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700441 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700442 unsigned long flags;
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700443 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700444
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700445 info = kmalloc(sizeof(*info) +
446 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400447 if (!info)
448 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700449
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700450 init_completion(&info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700451
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800452 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700453
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800454 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
455 msg->child_relid = channel->offermsg.child_relid;
456 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700457
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800458 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800459 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800460 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800461 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc6977672011-01-26 12:12:08 -0800462 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700463 sizeof(struct vmbus_channel_gpadl_teardown));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700464
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700465 if (ret)
466 goto post_msg_err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700467
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700468 wait_for_completion(&info->waitevent);
469
470post_msg_err:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800471 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800472 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800473 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700474
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700475 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700476 return ret;
477}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700478EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700479
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700480static void reset_channel_cb(void *arg)
481{
482 struct vmbus_channel *channel = arg;
483
484 channel->onchannel_callback = NULL;
485}
486
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700487static int vmbus_close_internal(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700488{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700489 struct vmbus_channel_close_channel *msg;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700490 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700491
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700492 channel->state = CHANNEL_OPEN_STATE;
493 channel->sc_creation_callback = NULL;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400494 /* Stop callback and cancel the timer asap */
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700495 if (channel->target_cpu != get_cpu()) {
496 put_cpu();
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700497 smp_call_function_single(channel->target_cpu, reset_channel_cb,
498 channel, true);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700499 } else {
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700500 reset_channel_cb(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700501 put_cpu();
502 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700503
Bill Pemberton454f18a2009-07-27 16:47:24 -0400504 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700505
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700506 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700507
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800508 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
509 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700510
Haiyang Zhangc6977672011-01-26 12:12:08 -0800511 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700512
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700513 if (ret) {
514 pr_err("Close failed: close post msg return is %d\n", ret);
515 /*
516 * If we failed to post the close msg,
517 * it is perhaps better to leak memory.
518 */
519 return ret;
520 }
521
Bill Pemberton454f18a2009-07-27 16:47:24 -0400522 /* Tear down the gpadl for the channel's ring buffer */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700523 if (channel->ringbuffer_gpadlhandle) {
524 ret = vmbus_teardown_gpadl(channel,
525 channel->ringbuffer_gpadlhandle);
526 if (ret) {
527 pr_err("Close failed: teardown gpadl return %d\n", ret);
528 /*
529 * If we failed to teardown gpadl,
530 * it is perhaps better to leak memory.
531 */
532 return ret;
533 }
534 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700535
Bill Pemberton454f18a2009-07-27 16:47:24 -0400536 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700537 hv_ringbuffer_cleanup(&channel->outbound);
538 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700539
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800540 free_pages((unsigned long)channel->ringbuffer_pages,
541 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700542
K. Y. Srinivasanf2eddbc2015-03-18 12:29:27 -0700543 /*
544 * If the channel has been rescinded; process device removal.
545 */
546 if (channel->rescind)
547 hv_process_channel_removal(channel,
548 channel->offermsg.child_relid);
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700549 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700550}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700551
552/*
553 * vmbus_close - Close the specified channel
554 */
555void vmbus_close(struct vmbus_channel *channel)
556{
557 struct list_head *cur, *tmp;
558 struct vmbus_channel *cur_channel;
559
560 if (channel->primary_channel != NULL) {
561 /*
562 * We will only close sub-channels when
563 * the primary is closed.
564 */
565 return;
566 }
567 /*
568 * Close all the sub-channels first and then close the
569 * primary channel.
570 */
571 list_for_each_safe(cur, tmp, &channel->sc_list) {
572 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
573 if (cur_channel->state != CHANNEL_OPENED_STATE)
574 continue;
575 vmbus_close_internal(cur_channel);
576 }
577 /*
578 * Now close the primary.
579 */
580 vmbus_close_internal(channel);
581}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700582EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700583
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800584int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700585 u32 bufferlen, u64 requestid,
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800586 enum vmbus_packet_type type, u32 flags, bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700587{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700588 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700589 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100590 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800591 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700592 u64 aligned_data = 0;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700593 int ret;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800594 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700595
Hank Janssen3e7ee492009-07-13 16:02:34 -0700596
Bill Pemberton454f18a2009-07-27 16:47:24 -0400597 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800598 desc.type = type; /* VmbusPacketTypeDataInBand; */
599 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700600 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800601 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
602 desc.len8 = (u16)(packetlen_aligned >> 3);
603 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700604
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800605 bufferlist[0].iov_base = &desc;
606 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
607 bufferlist[1].iov_base = buffer;
608 bufferlist[1].iov_len = bufferlen;
609 bufferlist[2].iov_base = &aligned_data;
610 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700611
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800612 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700613
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700614 /*
615 * Signalling the host is conditional on many factors:
616 * 1. The ring state changed from being empty to non-empty.
617 * This is tracked by the variable "signal".
618 * 2. The variable kick_q tracks if more data will be placed
619 * on the ring. We will not signal if more data is
620 * to be placed.
621 *
622 * If we cannot write to the ring-buffer; signal the host
623 * even if we may not have written anything. This is a rare
624 * enough condition that it should not matter.
625 */
626 if (((ret == 0) && kick_q && signal) || (ret))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700627 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700628
Hank Janssen3e7ee492009-07-13 16:02:34 -0700629 return ret;
630}
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800631EXPORT_SYMBOL(vmbus_sendpacket_ctl);
632
633/**
634 * vmbus_sendpacket() - Send the specified buffer on the given channel
635 * @channel: Pointer to vmbus_channel structure.
636 * @buffer: Pointer to the buffer you want to receive the data into.
637 * @bufferlen: Maximum size of what the the buffer will hold
638 * @requestid: Identifier of the request
639 * @type: Type of packet that is being send e.g. negotiate, time
640 * packet etc.
641 *
642 * Sends data in @buffer directly to hyper-v via the vmbus
643 * This will send the data unparsed to hyper-v.
644 *
645 * Mainly used by Hyper-V drivers.
646 */
647int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
648 u32 bufferlen, u64 requestid,
649 enum vmbus_packet_type type, u32 flags)
650{
651 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
652 type, flags, true);
653}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700654EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700655
Hank Janssen3e189512010-03-04 22:11:00 +0000656/*
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800657 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
658 * packets using a GPADL Direct packet type. This interface allows you
659 * to control notifying the host. This will be useful for sending
660 * batched data. Also the sender can control the send flags
661 * explicitly.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700662 */
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800663int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700664 struct hv_page_buffer pagebuffers[],
665 u32 pagecount, void *buffer, u32 bufferlen,
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800666 u64 requestid,
667 u32 flags,
668 bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700669{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700670 int ret;
671 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000672 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700673 u32 descsize;
674 u32 packetlen;
675 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800676 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700677 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800678 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700679
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700680 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400681 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700682
Hank Janssen3e7ee492009-07-13 16:02:34 -0700683
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700684 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000685 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700686 * largest size we support
687 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700688 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
689 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700690 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700691 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100692 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700693
Bill Pemberton454f18a2009-07-27 16:47:24 -0400694 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800695 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800696 desc.flags = flags;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700697 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
698 desc.length8 = (u16)(packetlen_aligned >> 3);
699 desc.transactionid = requestid;
700 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700701
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700702 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800703 desc.range[i].len = pagebuffers[i].len;
704 desc.range[i].offset = pagebuffers[i].offset;
705 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700706 }
707
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800708 bufferlist[0].iov_base = &desc;
709 bufferlist[0].iov_len = descsize;
710 bufferlist[1].iov_base = buffer;
711 bufferlist[1].iov_len = bufferlen;
712 bufferlist[2].iov_base = &aligned_data;
713 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700714
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800715 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700716
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700717 /*
718 * Signalling the host is conditional on many factors:
719 * 1. The ring state changed from being empty to non-empty.
720 * This is tracked by the variable "signal".
721 * 2. The variable kick_q tracks if more data will be placed
722 * on the ring. We will not signal if more data is
723 * to be placed.
724 *
725 * If we cannot write to the ring-buffer; signal the host
726 * even if we may not have written anything. This is a rare
727 * enough condition that it should not matter.
728 */
729 if (((ret == 0) && kick_q && signal) || (ret))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700730 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700731
Hank Janssen3e7ee492009-07-13 16:02:34 -0700732 return ret;
733}
K. Y. Srinivasanb3a19b32015-03-18 12:29:29 -0700734EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800735
736/*
737 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
738 * packets using a GPADL Direct packet type.
739 */
740int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
741 struct hv_page_buffer pagebuffers[],
742 u32 pagecount, void *buffer, u32 bufferlen,
743 u64 requestid)
744{
745 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
746 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
747 buffer, bufferlen, requestid,
748 flags, true);
749
750}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700751EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700752
Hank Janssen3e189512010-03-04 22:11:00 +0000753/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700754 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000755 * using a GPADL Direct packet type.
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800756 * The buffer includes the vmbus descriptor.
757 */
758int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
759 struct vmbus_packet_mpb_array *desc,
760 u32 desc_size,
761 void *buffer, u32 bufferlen, u64 requestid)
762{
763 int ret;
764 u32 packetlen;
765 u32 packetlen_aligned;
766 struct kvec bufferlist[3];
767 u64 aligned_data = 0;
768 bool signal = false;
769
770 packetlen = desc_size + bufferlen;
771 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
772
773 /* Setup the descriptor */
774 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
775 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
776 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
777 desc->length8 = (u16)(packetlen_aligned >> 3);
778 desc->transactionid = requestid;
779 desc->rangecount = 1;
780
781 bufferlist[0].iov_base = desc;
782 bufferlist[0].iov_len = desc_size;
783 bufferlist[1].iov_base = buffer;
784 bufferlist[1].iov_len = bufferlen;
785 bufferlist[2].iov_base = &aligned_data;
786 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
787
788 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
789
790 if (ret == 0 && signal)
791 vmbus_setevent(channel);
792
793 return ret;
794}
795EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
796
797/*
798 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
799 * using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700800 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700801int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700802 struct hv_multipage_buffer *multi_pagebuffer,
803 void *buffer, u32 bufferlen, u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700804{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700805 int ret;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000806 struct vmbus_channel_packet_multipage_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700807 u32 descsize;
808 u32 packetlen;
809 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800810 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700811 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800812 bool signal = false;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800813 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
814 multi_pagebuffer->len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700815
Tobias Klauser24b8a402014-04-25 17:03:50 +0200816 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400817 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700818
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700819 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000820 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700821 * the largest size we support
822 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700823 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
824 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700825 sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700826 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100827 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700828
Hank Janssen3e7ee492009-07-13 16:02:34 -0700829
Bill Pemberton454f18a2009-07-27 16:47:24 -0400830 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800831 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000832 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700833 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
834 desc.length8 = (u16)(packetlen_aligned >> 3);
835 desc.transactionid = requestid;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000836 desc.rangecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700837
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800838 desc.range.len = multi_pagebuffer->len;
839 desc.range.offset = multi_pagebuffer->offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700840
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800841 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700842 pfncount * sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700843
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800844 bufferlist[0].iov_base = &desc;
845 bufferlist[0].iov_len = descsize;
846 bufferlist[1].iov_base = buffer;
847 bufferlist[1].iov_len = bufferlen;
848 bufferlist[2].iov_base = &aligned_data;
849 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700850
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800851 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700852
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800853 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700854 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700855
Hank Janssen3e7ee492009-07-13 16:02:34 -0700856 return ret;
857}
Greg Kroah-Hartman4cb106f2010-10-21 09:23:59 -0700858EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700859
860/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700861 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700862 * @channel: Pointer to vmbus_channel structure.
863 * @buffer: Pointer to the buffer you want to receive the data into.
864 * @bufferlen: Maximum size of what the the buffer will hold
865 * @buffer_actual_len: The actual size of the data after it was received
866 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700867 *
868 * Receives directly from the hyper-v vmbus and puts the data it received
869 * into Buffer. This will receive the data unparsed from hyper-v.
870 *
871 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700872 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700873int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700874 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700875{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700876 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700877 u32 packetlen;
878 u32 userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700879 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800880 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700881
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700882 *buffer_actual_len = 0;
883 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700884
Hank Janssen3e7ee492009-07-13 16:02:34 -0700885
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700886 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700887 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700888 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700889 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700890
Haiyang Zhang415f2282011-01-26 12:12:13 -0800891 packetlen = desc.len8 << 3;
892 userlen = packetlen - (desc.offset8 << 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700893
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700894 *buffer_actual_len = userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700895
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700896 if (userlen > bufferlen) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700897
Hank Janssen0a466182011-03-29 13:58:47 -0700898 pr_err("Buffer too small - got %d needs %d\n",
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700899 bufferlen, userlen);
K. Y. Srinivasan926ae5262011-06-06 15:50:11 -0700900 return -ETOOSMALL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700901 }
902
Haiyang Zhang415f2282011-01-26 12:12:13 -0800903 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700904
Bill Pemberton454f18a2009-07-27 16:47:24 -0400905 /* Copy over the packet to the user buffer */
K. Y. Srinivasan38397c82011-05-10 07:55:25 -0700906 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800907 (desc.offset8 << 3), &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700908
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800909 if (signal)
910 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700911
Hank Janssen3e7ee492009-07-13 16:02:34 -0700912 return 0;
913}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700914EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700915
Hank Janssen3e189512010-03-04 22:11:00 +0000916/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700917 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700918 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700919int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700920 u32 bufferlen, u32 *buffer_actual_len,
921 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700922{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700923 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700924 u32 packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700925 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800926 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700927
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700928 *buffer_actual_len = 0;
929 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700930
Hank Janssen3e7ee492009-07-13 16:02:34 -0700931
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700932 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700933 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700934 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700935 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700936
Hank Janssen3e7ee492009-07-13 16:02:34 -0700937
Haiyang Zhang415f2282011-01-26 12:12:13 -0800938 packetlen = desc.len8 << 3;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700939
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700940 *buffer_actual_len = packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700941
Jason Wang7a446d62014-06-30 14:14:48 +0800942 if (packetlen > bufferlen)
K. Y. Srinivasan3d5cad92011-08-25 09:48:59 -0700943 return -ENOBUFS;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700944
Haiyang Zhang415f2282011-01-26 12:12:13 -0800945 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700946
Bill Pemberton454f18a2009-07-27 16:47:24 -0400947 /* Copy over the entire packet to the user buffer */
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800948 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
949 &signal);
950
951 if (signal)
952 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700953
Dan Carpenter33b06932013-09-17 00:58:36 +0300954 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700955}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700956EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);