blob: c4dcab048cb8ecbc38b7784408bfd7902d5a40a7 [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;
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070076 struct page *page;
Hank Janssen3e7ee492009-07-13 16:02:34 -070077
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010078 spin_lock_irqsave(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070079 if (newchannel->state == CHANNEL_OPEN_STATE) {
80 newchannel->state = CHANNEL_OPENING_STATE;
81 } else {
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010082 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070083 return -EINVAL;
84 }
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010085 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070086
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080087 newchannel->onchannel_callback = onchannelcallback;
88 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070089
Bill Pemberton454f18a2009-07-27 16:47:24 -040090 /* Allocate the ring buffer */
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070091 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
92 GFP_KERNEL|__GFP_ZERO,
93 get_order(send_ringbuffer_size +
94 recv_ringbuffer_size));
95
96 if (!page)
97 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
98 get_order(send_ringbuffer_size +
99 recv_ringbuffer_size));
100 else
101 out = (void *)page_address(page);
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800102
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800103 if (!out) {
104 err = -ENOMEM;
105 goto error0;
106 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700107
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700108 in = (void *)((unsigned long)out + send_ringbuffer_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700109
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800110 newchannel->ringbuffer_pages = out;
111 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700112 recv_ringbuffer_size) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700113
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700114 ret = hv_ringbuffer_init(
115 &newchannel->outbound, out, send_ringbuffer_size);
116
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000117 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400118 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700119 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400120 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700121
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700122 ret = hv_ringbuffer_init(
123 &newchannel->inbound, in, recv_ringbuffer_size);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000124 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400125 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700126 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400127 }
128
Hank Janssen3e7ee492009-07-13 16:02:34 -0700129
Bill Pemberton454f18a2009-07-27 16:47:24 -0400130 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800131 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700132
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700133 ret = vmbus_establish_gpadl(newchannel,
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800134 newchannel->outbound.ring_buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700135 send_ringbuffer_size +
136 recv_ringbuffer_size,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800137 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400138
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000139 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400140 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700141 goto error0;
Bill Pembertonb94ef342010-05-05 15:27:37 -0400142 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700143
Bill Pemberton454f18a2009-07-27 16:47:24 -0400144 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700145 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700146 sizeof(struct vmbus_channel_open_channel),
147 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700148 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400149 err = -ENOMEM;
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800150 goto error_gpadl;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400151 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700152
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700153 init_completion(&open_info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700154
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700155 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700156 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
157 open_msg->openid = newchannel->offermsg.child_relid;
158 open_msg->child_relid = newchannel->offermsg.child_relid;
159 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
160 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700161 PAGE_SHIFT;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800162 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700163
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700164 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400165 err = -EINVAL;
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800166 goto error_gpadl;
Bill Pembertonc827f942010-05-05 15:27:38 -0400167 }
168
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700169 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700170 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700171
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800172 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700173 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800174 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800175 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700176
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700177 ret = vmbus_post_msg(open_msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700178 sizeof(struct vmbus_channel_open_channel));
Hank Janssen98e08702011-03-29 13:58:44 -0700179
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700180 if (ret != 0) {
181 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700182 goto error1;
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700183 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700184
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700185 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700186 if (t == 0) {
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800187 err = -ETIMEDOUT;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700188 goto error1;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800189 }
190
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800191 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700192 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800193 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700194
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700195 if (open_info->response.open_result.status) {
196 err = -EAGAIN;
197 goto error_gpadl;
198 }
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700199
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700200 newchannel->state = CHANNEL_OPENED_STATE;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700201 kfree(open_info);
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700202 return 0;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400203
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700204error1:
205 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
206 list_del(&open_info->msglistentry);
207 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
208
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800209error_gpadl:
210 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
211
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700212error0:
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800213 free_pages((unsigned long)out,
214 get_order(send_ringbuffer_size + recv_ringbuffer_size));
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700215 kfree(open_info);
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800216 newchannel->state = CHANNEL_OPEN_STATE;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400217 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700218}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700219EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700220
Hank Janssen3e189512010-03-04 22:11:00 +0000221/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700222 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700223 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700224static int create_gpadl_header(void *kbuffer, u32 size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700225 struct vmbus_channel_msginfo **msginfo,
226 u32 *messagecount)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700227{
228 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700229 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700230 struct vmbus_channel_gpadl_header *gpadl_header;
231 struct vmbus_channel_gpadl_body *gpadl_body;
232 struct vmbus_channel_msginfo *msgheader;
233 struct vmbus_channel_msginfo *msgbody = NULL;
234 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700235
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700236 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700237
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700238 pagecount = size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700239
Bill Pemberton454f18a2009-07-27 16:47:24 -0400240 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700241 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700242 sizeof(struct vmbus_channel_gpadl_header) -
243 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700244 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700245
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700246 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700247 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400248 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700249 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700250 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700251 sizeof(struct gpa_range) + pfncount * sizeof(u64);
252 msgheader = kzalloc(msgsize, GFP_KERNEL);
253 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400254 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700255
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800256 INIT_LIST_HEAD(&msgheader->submsglist);
257 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700258
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700259 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800260 msgheader->msg;
261 gpadl_header->rangecount = 1;
262 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700263 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800264 gpadl_header->range[0].byte_offset = 0;
265 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700266 for (i = 0; i < pfncount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800267 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
268 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700269 *msginfo = msgheader;
270 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700271
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700272 pfnsum = pfncount;
273 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700274
Bill Pemberton454f18a2009-07-27 16:47:24 -0400275 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700276 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700277 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700278 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700279
Bill Pemberton454f18a2009-07-27 16:47:24 -0400280 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700281 while (pfnleft) {
282 if (pfnleft > pfncount)
283 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700284 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700285 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700286
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700287 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700288 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700289 pfncurr * sizeof(u64);
290 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700291
292 if (!msgbody) {
293 struct vmbus_channel_msginfo *pos = NULL;
294 struct vmbus_channel_msginfo *tmp = NULL;
295 /*
296 * Free up all the allocated messages.
297 */
298 list_for_each_entry_safe(pos, tmp,
299 &msgheader->submsglist,
300 msglistentry) {
301
302 list_del(&pos->msglistentry);
303 kfree(pos);
304 }
305
Bill Pembertond1c250b2010-05-05 15:27:35 -0400306 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700307 }
308
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800309 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700310 (*messagecount)++;
311 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800312 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700313
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700314 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700315 * Gpadl is u32 and we are using a pointer which could
316 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700317 * This is governed by the guest/host protocol and
318 * so the hypervisor gurantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700319 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700320 for (i = 0; i < pfncurr; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800321 gpadl_body->pfn[i] = slow_virt_to_phys(
322 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
323 PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700324
Bill Pemberton454f18a2009-07-27 16:47:24 -0400325 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800326 list_add_tail(&msgbody->msglistentry,
327 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700328 pfnsum += pfncurr;
329 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700330 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700331 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400332 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700333 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700334 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700335 sizeof(struct gpa_range) + pagecount * sizeof(u64);
336 msgheader = kzalloc(msgsize, GFP_KERNEL);
337 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400338 goto nomem;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800339 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700340
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700341 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800342 msgheader->msg;
343 gpadl_header->rangecount = 1;
344 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700345 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800346 gpadl_header->range[0].byte_offset = 0;
347 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700348 for (i = 0; i < pagecount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800349 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
350 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700351
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700352 *msginfo = msgheader;
353 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700354 }
355
356 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400357nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700358 kfree(msgheader);
359 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400360 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700361}
362
Hank Janssen3e189512010-03-04 22:11:00 +0000363/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700364 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700365 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700366 * @channel: a channel
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800367 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700368 * @size: page-size multiple
369 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700370 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700371int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700372 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700373{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700374 struct vmbus_channel_gpadl_header *gpadlmsg;
375 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700376 struct vmbus_channel_msginfo *msginfo = NULL;
377 struct vmbus_channel_msginfo *submsginfo;
378 u32 msgcount;
Bill Pemberton53af5452009-09-11 21:46:44 -0400379 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700380 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700381 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400382 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700383
K. Y. Srinivasan9f52a162015-01-09 23:54:33 -0800384 next_gpadl_handle =
385 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700386
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700387 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400388 if (ret)
389 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700390
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700391 init_completion(&msginfo->waitevent);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400392
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800393 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
394 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
395 gpadlmsg->child_relid = channel->offermsg.child_relid;
396 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700397
Hank Janssen3e7ee492009-07-13 16:02:34 -0700398
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800399 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800400 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800401 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700402
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800403 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700404
Haiyang Zhangc6977672011-01-26 12:12:08 -0800405 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700406 sizeof(*msginfo));
Hank Janssen98e08702011-03-29 13:58:44 -0700407 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700408 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700409
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700410 if (msgcount > 1) {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800411 list_for_each(curr, &msginfo->submsglist) {
Bill Pemberton53af5452009-09-11 21:46:44 -0400412
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700413 submsginfo = (struct vmbus_channel_msginfo *)curr;
414 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800415 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700416
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800417 gpadl_body->header.msgtype =
418 CHANNELMSG_GPADL_BODY;
419 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700420
Haiyang Zhangc6977672011-01-26 12:12:08 -0800421 ret = vmbus_post_msg(gpadl_body,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800422 submsginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700423 sizeof(*submsginfo));
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000424 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700425 goto cleanup;
Bill Pemberton99259152010-05-05 15:27:36 -0400426
Hank Janssen3e7ee492009-07-13 16:02:34 -0700427 }
428 }
K. Y. Srinivasan72c6b712014-08-27 16:25:34 -0700429 wait_for_completion(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700430
Bill Pemberton454f18a2009-07-27 16:47:24 -0400431 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800432 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700433
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700434cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800435 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800436 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800437 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700438
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700439 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700440 return ret;
441}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700442EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700443
Hank Janssen3e189512010-03-04 22:11:00 +0000444/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700445 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700446 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700447int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700448{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700449 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700450 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700451 unsigned long flags;
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700452 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700453
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700454 info = kmalloc(sizeof(*info) +
455 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400456 if (!info)
457 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700458
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700459 init_completion(&info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700460
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800461 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700462
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800463 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
464 msg->child_relid = channel->offermsg.child_relid;
465 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700466
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800467 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800468 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800469 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800470 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc6977672011-01-26 12:12:08 -0800471 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700472 sizeof(struct vmbus_channel_gpadl_teardown));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700473
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700474 if (ret)
475 goto post_msg_err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700476
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700477 wait_for_completion(&info->waitevent);
478
479post_msg_err:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800480 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800481 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800482 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700483
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700484 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700485 return ret;
486}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700487EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700488
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700489static void reset_channel_cb(void *arg)
490{
491 struct vmbus_channel *channel = arg;
492
493 channel->onchannel_callback = NULL;
494}
495
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700496static int vmbus_close_internal(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700497{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700498 struct vmbus_channel_close_channel *msg;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700499 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700500
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700501 channel->state = CHANNEL_OPEN_STATE;
502 channel->sc_creation_callback = NULL;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400503 /* Stop callback and cancel the timer asap */
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700504 if (channel->target_cpu != get_cpu()) {
505 put_cpu();
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700506 smp_call_function_single(channel->target_cpu, reset_channel_cb,
507 channel, true);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700508 } else {
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700509 reset_channel_cb(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700510 put_cpu();
511 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700512
Bill Pemberton454f18a2009-07-27 16:47:24 -0400513 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700514
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700515 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700516
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800517 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
518 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700519
Haiyang Zhangc6977672011-01-26 12:12:08 -0800520 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700521
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700522 if (ret) {
523 pr_err("Close failed: close post msg return is %d\n", ret);
524 /*
525 * If we failed to post the close msg,
526 * it is perhaps better to leak memory.
527 */
528 return ret;
529 }
530
Bill Pemberton454f18a2009-07-27 16:47:24 -0400531 /* Tear down the gpadl for the channel's ring buffer */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700532 if (channel->ringbuffer_gpadlhandle) {
533 ret = vmbus_teardown_gpadl(channel,
534 channel->ringbuffer_gpadlhandle);
535 if (ret) {
536 pr_err("Close failed: teardown gpadl return %d\n", ret);
537 /*
538 * If we failed to teardown gpadl,
539 * it is perhaps better to leak memory.
540 */
541 return ret;
542 }
543 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700544
Bill Pemberton454f18a2009-07-27 16:47:24 -0400545 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700546 hv_ringbuffer_cleanup(&channel->outbound);
547 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700548
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800549 free_pages((unsigned long)channel->ringbuffer_pages,
550 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700551
K. Y. Srinivasanf2eddbc2015-03-18 12:29:27 -0700552 /*
553 * If the channel has been rescinded; process device removal.
554 */
555 if (channel->rescind)
556 hv_process_channel_removal(channel,
557 channel->offermsg.child_relid);
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700558 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700559}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700560
561/*
562 * vmbus_close - Close the specified channel
563 */
564void vmbus_close(struct vmbus_channel *channel)
565{
566 struct list_head *cur, *tmp;
567 struct vmbus_channel *cur_channel;
568
569 if (channel->primary_channel != NULL) {
570 /*
571 * We will only close sub-channels when
572 * the primary is closed.
573 */
574 return;
575 }
576 /*
577 * Close all the sub-channels first and then close the
578 * primary channel.
579 */
580 list_for_each_safe(cur, tmp, &channel->sc_list) {
581 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
582 if (cur_channel->state != CHANNEL_OPENED_STATE)
583 continue;
584 vmbus_close_internal(cur_channel);
585 }
586 /*
587 * Now close the primary.
588 */
589 vmbus_close_internal(channel);
590}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700591EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700592
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800593int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700594 u32 bufferlen, u64 requestid,
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800595 enum vmbus_packet_type type, u32 flags, bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700596{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700597 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700598 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100599 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800600 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700601 u64 aligned_data = 0;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700602 int ret;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800603 bool signal = false;
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700604 int num_vecs = ((bufferlen != 0) ? 3 : 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700605
Hank Janssen3e7ee492009-07-13 16:02:34 -0700606
Bill Pemberton454f18a2009-07-27 16:47:24 -0400607 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800608 desc.type = type; /* VmbusPacketTypeDataInBand; */
609 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700610 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800611 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
612 desc.len8 = (u16)(packetlen_aligned >> 3);
613 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700614
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800615 bufferlist[0].iov_base = &desc;
616 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
617 bufferlist[1].iov_base = buffer;
618 bufferlist[1].iov_len = bufferlen;
619 bufferlist[2].iov_base = &aligned_data;
620 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700621
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700622 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, num_vecs,
623 &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700624
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700625 /*
626 * Signalling the host is conditional on many factors:
627 * 1. The ring state changed from being empty to non-empty.
628 * This is tracked by the variable "signal".
629 * 2. The variable kick_q tracks if more data will be placed
630 * on the ring. We will not signal if more data is
631 * to be placed.
632 *
633 * If we cannot write to the ring-buffer; signal the host
634 * even if we may not have written anything. This is a rare
635 * enough condition that it should not matter.
636 */
637 if (((ret == 0) && kick_q && signal) || (ret))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700638 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700639
Hank Janssen3e7ee492009-07-13 16:02:34 -0700640 return ret;
641}
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800642EXPORT_SYMBOL(vmbus_sendpacket_ctl);
643
644/**
645 * vmbus_sendpacket() - Send the specified buffer on the given channel
646 * @channel: Pointer to vmbus_channel structure.
647 * @buffer: Pointer to the buffer you want to receive the data into.
648 * @bufferlen: Maximum size of what the the buffer will hold
649 * @requestid: Identifier of the request
650 * @type: Type of packet that is being send e.g. negotiate, time
651 * packet etc.
652 *
653 * Sends data in @buffer directly to hyper-v via the vmbus
654 * This will send the data unparsed to hyper-v.
655 *
656 * Mainly used by Hyper-V drivers.
657 */
658int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
659 u32 bufferlen, u64 requestid,
660 enum vmbus_packet_type type, u32 flags)
661{
662 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
663 type, flags, true);
664}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700665EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700666
Hank Janssen3e189512010-03-04 22:11:00 +0000667/*
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800668 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
669 * packets using a GPADL Direct packet type. This interface allows you
670 * to control notifying the host. This will be useful for sending
671 * batched data. Also the sender can control the send flags
672 * explicitly.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700673 */
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800674int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700675 struct hv_page_buffer pagebuffers[],
676 u32 pagecount, void *buffer, u32 bufferlen,
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800677 u64 requestid,
678 u32 flags,
679 bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700680{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700681 int ret;
682 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000683 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700684 u32 descsize;
685 u32 packetlen;
686 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800687 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700688 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800689 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700690
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700691 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400692 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700693
Hank Janssen3e7ee492009-07-13 16:02:34 -0700694
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700695 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000696 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700697 * largest size we support
698 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700699 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
700 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700701 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700702 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100703 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700704
Bill Pemberton454f18a2009-07-27 16:47:24 -0400705 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800706 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800707 desc.flags = flags;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700708 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
709 desc.length8 = (u16)(packetlen_aligned >> 3);
710 desc.transactionid = requestid;
711 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700712
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700713 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800714 desc.range[i].len = pagebuffers[i].len;
715 desc.range[i].offset = pagebuffers[i].offset;
716 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700717 }
718
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800719 bufferlist[0].iov_base = &desc;
720 bufferlist[0].iov_len = descsize;
721 bufferlist[1].iov_base = buffer;
722 bufferlist[1].iov_len = bufferlen;
723 bufferlist[2].iov_base = &aligned_data;
724 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700725
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800726 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700727
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700728 /*
729 * Signalling the host is conditional on many factors:
730 * 1. The ring state changed from being empty to non-empty.
731 * This is tracked by the variable "signal".
732 * 2. The variable kick_q tracks if more data will be placed
733 * on the ring. We will not signal if more data is
734 * to be placed.
735 *
736 * If we cannot write to the ring-buffer; signal the host
737 * even if we may not have written anything. This is a rare
738 * enough condition that it should not matter.
739 */
740 if (((ret == 0) && kick_q && signal) || (ret))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700741 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700742
Hank Janssen3e7ee492009-07-13 16:02:34 -0700743 return ret;
744}
K. Y. Srinivasanb3a19b32015-03-18 12:29:29 -0700745EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800746
747/*
748 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
749 * packets using a GPADL Direct packet type.
750 */
751int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
752 struct hv_page_buffer pagebuffers[],
753 u32 pagecount, void *buffer, u32 bufferlen,
754 u64 requestid)
755{
756 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
757 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
758 buffer, bufferlen, requestid,
759 flags, true);
760
761}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700762EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700763
Hank Janssen3e189512010-03-04 22:11:00 +0000764/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700765 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000766 * using a GPADL Direct packet type.
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800767 * The buffer includes the vmbus descriptor.
768 */
769int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
770 struct vmbus_packet_mpb_array *desc,
771 u32 desc_size,
772 void *buffer, u32 bufferlen, u64 requestid)
773{
774 int ret;
775 u32 packetlen;
776 u32 packetlen_aligned;
777 struct kvec bufferlist[3];
778 u64 aligned_data = 0;
779 bool signal = false;
780
781 packetlen = desc_size + bufferlen;
782 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
783
784 /* Setup the descriptor */
785 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
786 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
787 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
788 desc->length8 = (u16)(packetlen_aligned >> 3);
789 desc->transactionid = requestid;
790 desc->rangecount = 1;
791
792 bufferlist[0].iov_base = desc;
793 bufferlist[0].iov_len = desc_size;
794 bufferlist[1].iov_base = buffer;
795 bufferlist[1].iov_len = bufferlen;
796 bufferlist[2].iov_base = &aligned_data;
797 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
798
799 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
800
801 if (ret == 0 && signal)
802 vmbus_setevent(channel);
803
804 return ret;
805}
806EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
807
808/*
809 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
810 * using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700811 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700812int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700813 struct hv_multipage_buffer *multi_pagebuffer,
814 void *buffer, u32 bufferlen, u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700815{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700816 int ret;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000817 struct vmbus_channel_packet_multipage_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700818 u32 descsize;
819 u32 packetlen;
820 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800821 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700822 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800823 bool signal = false;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800824 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
825 multi_pagebuffer->len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700826
Tobias Klauser24b8a402014-04-25 17:03:50 +0200827 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400828 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700829
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700830 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000831 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700832 * the largest size we support
833 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700834 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
835 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700836 sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700837 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100838 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700839
Hank Janssen3e7ee492009-07-13 16:02:34 -0700840
Bill Pemberton454f18a2009-07-27 16:47:24 -0400841 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800842 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000843 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700844 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
845 desc.length8 = (u16)(packetlen_aligned >> 3);
846 desc.transactionid = requestid;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000847 desc.rangecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700848
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800849 desc.range.len = multi_pagebuffer->len;
850 desc.range.offset = multi_pagebuffer->offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700851
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800852 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700853 pfncount * sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700854
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800855 bufferlist[0].iov_base = &desc;
856 bufferlist[0].iov_len = descsize;
857 bufferlist[1].iov_base = buffer;
858 bufferlist[1].iov_len = bufferlen;
859 bufferlist[2].iov_base = &aligned_data;
860 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700861
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800862 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700863
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800864 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700865 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700866
Hank Janssen3e7ee492009-07-13 16:02:34 -0700867 return ret;
868}
Greg Kroah-Hartman4cb106f2010-10-21 09:23:59 -0700869EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700870
871/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700872 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700873 * @channel: Pointer to vmbus_channel structure.
874 * @buffer: Pointer to the buffer you want to receive the data into.
875 * @bufferlen: Maximum size of what the the buffer will hold
876 * @buffer_actual_len: The actual size of the data after it was received
877 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700878 *
879 * Receives directly from the hyper-v vmbus and puts the data it received
880 * into Buffer. This will receive the data unparsed from hyper-v.
881 *
882 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700883 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700884int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700885 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700886{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700887 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700888 u32 packetlen;
889 u32 userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700890 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800891 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700892
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700893 *buffer_actual_len = 0;
894 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700895
Hank Janssen3e7ee492009-07-13 16:02:34 -0700896
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700897 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700898 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700899 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700900 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700901
Haiyang Zhang415f2282011-01-26 12:12:13 -0800902 packetlen = desc.len8 << 3;
903 userlen = packetlen - (desc.offset8 << 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700904
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700905 *buffer_actual_len = userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700906
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700907 if (userlen > bufferlen) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700908
Hank Janssen0a466182011-03-29 13:58:47 -0700909 pr_err("Buffer too small - got %d needs %d\n",
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700910 bufferlen, userlen);
K. Y. Srinivasan926ae5262011-06-06 15:50:11 -0700911 return -ETOOSMALL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700912 }
913
Haiyang Zhang415f2282011-01-26 12:12:13 -0800914 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700915
Bill Pemberton454f18a2009-07-27 16:47:24 -0400916 /* Copy over the packet to the user buffer */
K. Y. Srinivasan38397c82011-05-10 07:55:25 -0700917 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800918 (desc.offset8 << 3), &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700919
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800920 if (signal)
921 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700922
Hank Janssen3e7ee492009-07-13 16:02:34 -0700923 return 0;
924}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700925EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700926
Hank Janssen3e189512010-03-04 22:11:00 +0000927/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700928 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700929 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700930int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700931 u32 bufferlen, u32 *buffer_actual_len,
932 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700933{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700934 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700935 u32 packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700936 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800937 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700938
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700939 *buffer_actual_len = 0;
940 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700941
Hank Janssen3e7ee492009-07-13 16:02:34 -0700942
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700943 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700944 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700945 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700946 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700947
Hank Janssen3e7ee492009-07-13 16:02:34 -0700948
Haiyang Zhang415f2282011-01-26 12:12:13 -0800949 packetlen = desc.len8 << 3;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700950
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700951 *buffer_actual_len = packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700952
Jason Wang7a446d62014-06-30 14:14:48 +0800953 if (packetlen > bufferlen)
K. Y. Srinivasan3d5cad92011-08-25 09:48:59 -0700954 return -ENOBUFS;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700955
Haiyang Zhang415f2282011-01-26 12:12:13 -0800956 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700957
Bill Pemberton454f18a2009-07-27 16:47:24 -0400958 /* Copy over the entire packet to the user buffer */
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800959 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
960 &signal);
961
962 if (signal)
963 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700964
Dan Carpenter33b06932013-09-17 00:58:36 +0300965 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700966}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700967EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);