blob: 2889d97c03b1d8da44dfb179715e78d59439f272 [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
Hank Janssen3e7ee492009-07-13 16:02:34 -07002 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssen3e7ee492009-07-13 16:02:34 -070020 */
Hank Janssen0a466182011-03-29 13:58:47 -070021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Greg Kroah-Hartman5654e932009-07-14 15:08:20 -070023#include <linux/kernel.h>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080024#include <linux/sched.h>
25#include <linux/wait.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070026#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Hank Janssenc88c4e42010-05-04 15:55:05 -070028#include <linux/module.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070029#include <linux/hyperv.h>
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -080030#include <linux/uio.h>
Dexuan Cui63d55b22015-12-14 16:01:47 -080031#include <linux/interrupt.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070032
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070033#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070034
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080035#define NUM_PAGES_SPANNED(addr, len) \
36((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
37
Hank Janssen3e189512010-03-04 22:11:00 +000038/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070039 * vmbus_setevent- Trigger an event notification on the specified
Hank Janssen3e189512010-03-04 22:11:00 +000040 * channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070041 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070042static void vmbus_setevent(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -070043{
Haiyang Zhang39d70a42010-09-30 10:52:13 -070044 struct hv_monitor_page *monitorpage;
Hank Janssen3e7ee492009-07-13 16:02:34 -070045
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080046 if (channel->offermsg.monitor_allocated) {
Bill Pemberton454f18a2009-07-27 16:47:24 -040047 /* Each u32 represents 32 channels */
Olaf Hering223565852011-03-21 14:41:37 +010048 sync_set_bit(channel->offermsg.child_relid & 31,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080049 (unsigned long *) vmbus_connection.send_int_page +
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080050 (channel->offermsg.child_relid >> 5));
Hank Janssen3e7ee492009-07-13 16:02:34 -070051
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -070052 /* Get the child to parent monitor page */
53 monitorpage = vmbus_connection.monitor_pages[1];
Hank Janssen3e7ee492009-07-13 16:02:34 -070054
Olaf Hering223565852011-03-21 14:41:37 +010055 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080056 (unsigned long *)&monitorpage->trigger_group
57 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -040058
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070059 } else {
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -080060 vmbus_set_event(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -070061 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070062}
63
Hank Janssen3e189512010-03-04 22:11:00 +000064/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070065 * vmbus_open - Open the specified channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070066 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070067int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -070068 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
69 void (*onchannelcallback)(void *context), void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -070070{
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -070071 struct vmbus_channel_open_channel *open_msg;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -070072 struct vmbus_channel_msginfo *open_info = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -070073 void *in, *out;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070074 unsigned long flags;
Nicholas Mc Guire08a95132015-02-27 11:26:01 -080075 int ret, err = 0;
76 unsigned long t;
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070077 struct page *page;
Hank Janssen3e7ee492009-07-13 16:02:34 -070078
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010079 spin_lock_irqsave(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070080 if (newchannel->state == CHANNEL_OPEN_STATE) {
81 newchannel->state = CHANNEL_OPENING_STATE;
82 } else {
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010083 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070084 return -EINVAL;
85 }
Vitaly Kuznetsov67fae052015-01-20 16:45:05 +010086 spin_unlock_irqrestore(&newchannel->lock, flags);
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070087
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080088 newchannel->onchannel_callback = onchannelcallback;
89 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070090
Bill Pemberton454f18a2009-07-27 16:47:24 -040091 /* Allocate the ring buffer */
K. Y. Srinivasan294409d2015-05-31 21:27:03 -070092 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
93 GFP_KERNEL|__GFP_ZERO,
94 get_order(send_ringbuffer_size +
95 recv_ringbuffer_size));
96
97 if (!page)
98 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
99 get_order(send_ringbuffer_size +
100 recv_ringbuffer_size));
101 else
102 out = (void *)page_address(page);
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800103
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800104 if (!out) {
105 err = -ENOMEM;
106 goto error0;
107 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700108
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700109 in = (void *)((unsigned long)out + send_ringbuffer_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700110
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800111 newchannel->ringbuffer_pages = out;
112 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700113 recv_ringbuffer_size) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700114
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700115 ret = hv_ringbuffer_init(
116 &newchannel->outbound, out, send_ringbuffer_size);
117
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000118 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400119 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700120 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400121 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700122
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700123 ret = hv_ringbuffer_init(
124 &newchannel->inbound, in, recv_ringbuffer_size);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000125 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400126 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700127 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400128 }
129
Hank Janssen3e7ee492009-07-13 16:02:34 -0700130
Bill Pemberton454f18a2009-07-27 16:47:24 -0400131 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800132 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700133
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700134 ret = vmbus_establish_gpadl(newchannel,
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800135 newchannel->outbound.ring_buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700136 send_ringbuffer_size +
137 recv_ringbuffer_size,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800138 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400139
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000140 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400141 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700142 goto error0;
Bill Pembertonb94ef342010-05-05 15:27:37 -0400143 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700144
Bill Pemberton454f18a2009-07-27 16:47:24 -0400145 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700146 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700147 sizeof(struct vmbus_channel_open_channel),
148 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700149 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400150 err = -ENOMEM;
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800151 goto error_gpadl;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400152 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700153
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700154 init_completion(&open_info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700155
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700156 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700157 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
158 open_msg->openid = newchannel->offermsg.child_relid;
159 open_msg->child_relid = newchannel->offermsg.child_relid;
160 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
161 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700162 PAGE_SHIFT;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800163 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700164
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700165 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400166 err = -EINVAL;
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800167 goto error_gpadl;
Bill Pembertonc827f942010-05-05 15:27:38 -0400168 }
169
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700170 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700171 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700172
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800173 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700174 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800175 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800176 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700177
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700178 ret = vmbus_post_msg(open_msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700179 sizeof(struct vmbus_channel_open_channel));
Hank Janssen98e08702011-03-29 13:58:44 -0700180
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700181 if (ret != 0) {
182 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700183 goto error1;
K. Y. Srinivasan45d727c2014-08-27 16:25:35 -0700184 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700185
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700186 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700187 if (t == 0) {
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800188 err = -ETIMEDOUT;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700189 goto error1;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800190 }
191
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800192 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700193 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800194 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700195
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700196 if (open_info->response.open_result.status) {
197 err = -EAGAIN;
198 goto error_gpadl;
199 }
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700200
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700201 newchannel->state = CHANNEL_OPENED_STATE;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700202 kfree(open_info);
Vitaly Kuznetsovffc151f2015-05-06 17:47:40 -0700203 return 0;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400204
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700205error1:
206 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
207 list_del(&open_info->msglistentry);
208 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
209
K. Y. Srinivasan40384e42015-02-27 11:26:04 -0800210error_gpadl:
211 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
212
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700213error0:
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800214 free_pages((unsigned long)out,
215 get_order(send_ringbuffer_size + recv_ringbuffer_size));
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700216 kfree(open_info);
Dexuan Cuiac0d12b2015-02-27 11:26:00 -0800217 newchannel->state = CHANNEL_OPEN_STATE;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400218 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700219}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700220EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700221
Hank Janssen3e189512010-03-04 22:11:00 +0000222/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700223 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700224 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700225static int create_gpadl_header(void *kbuffer, u32 size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700226 struct vmbus_channel_msginfo **msginfo,
227 u32 *messagecount)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700228{
229 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700230 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700231 struct vmbus_channel_gpadl_header *gpadl_header;
232 struct vmbus_channel_gpadl_body *gpadl_body;
233 struct vmbus_channel_msginfo *msgheader;
234 struct vmbus_channel_msginfo *msgbody = NULL;
235 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700236
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700237 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700238
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700239 pagecount = size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700240
Bill Pemberton454f18a2009-07-27 16:47:24 -0400241 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700242 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700243 sizeof(struct vmbus_channel_gpadl_header) -
244 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700245 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700246
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700247 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700248 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400249 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700250 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700251 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700252 sizeof(struct gpa_range) + pfncount * sizeof(u64);
253 msgheader = kzalloc(msgsize, GFP_KERNEL);
254 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400255 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700256
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800257 INIT_LIST_HEAD(&msgheader->submsglist);
258 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700259
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700260 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800261 msgheader->msg;
262 gpadl_header->rangecount = 1;
263 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700264 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800265 gpadl_header->range[0].byte_offset = 0;
266 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700267 for (i = 0; i < pfncount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800268 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
269 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700270 *msginfo = msgheader;
271 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700272
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700273 pfnsum = pfncount;
274 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700275
Bill Pemberton454f18a2009-07-27 16:47:24 -0400276 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700277 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700278 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700279 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700280
Bill Pemberton454f18a2009-07-27 16:47:24 -0400281 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700282 while (pfnleft) {
283 if (pfnleft > pfncount)
284 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700285 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700286 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700287
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700288 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700289 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700290 pfncurr * sizeof(u64);
291 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700292
293 if (!msgbody) {
294 struct vmbus_channel_msginfo *pos = NULL;
295 struct vmbus_channel_msginfo *tmp = NULL;
296 /*
297 * Free up all the allocated messages.
298 */
299 list_for_each_entry_safe(pos, tmp,
300 &msgheader->submsglist,
301 msglistentry) {
302
303 list_del(&pos->msglistentry);
304 kfree(pos);
305 }
306
Bill Pembertond1c250b2010-05-05 15:27:35 -0400307 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700308 }
309
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800310 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700311 (*messagecount)++;
312 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800313 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700314
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700315 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700316 * Gpadl is u32 and we are using a pointer which could
317 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700318 * This is governed by the guest/host protocol and
319 * so the hypervisor gurantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700320 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700321 for (i = 0; i < pfncurr; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800322 gpadl_body->pfn[i] = slow_virt_to_phys(
323 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
324 PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700325
Bill Pemberton454f18a2009-07-27 16:47:24 -0400326 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800327 list_add_tail(&msgbody->msglistentry,
328 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700329 pfnsum += pfncurr;
330 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700331 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700332 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400333 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700334 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700335 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700336 sizeof(struct gpa_range) + pagecount * sizeof(u64);
337 msgheader = kzalloc(msgsize, GFP_KERNEL);
338 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400339 goto nomem;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800340 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700341
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700342 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800343 msgheader->msg;
344 gpadl_header->rangecount = 1;
345 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700346 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800347 gpadl_header->range[0].byte_offset = 0;
348 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700349 for (i = 0; i < pagecount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800350 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
351 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700352
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700353 *msginfo = msgheader;
354 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700355 }
356
357 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400358nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700359 kfree(msgheader);
360 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400361 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700362}
363
Hank Janssen3e189512010-03-04 22:11:00 +0000364/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700365 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700366 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700367 * @channel: a channel
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800368 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700369 * @size: page-size multiple
370 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700371 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700372int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700373 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700374{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700375 struct vmbus_channel_gpadl_header *gpadlmsg;
376 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700377 struct vmbus_channel_msginfo *msginfo = NULL;
378 struct vmbus_channel_msginfo *submsginfo;
379 u32 msgcount;
Bill Pemberton53af5452009-09-11 21:46:44 -0400380 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700381 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700382 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400383 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700384
K. Y. Srinivasan9f52a162015-01-09 23:54:33 -0800385 next_gpadl_handle =
386 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700387
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700388 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400389 if (ret)
390 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700391
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700392 init_completion(&msginfo->waitevent);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400393
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800394 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
395 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
396 gpadlmsg->child_relid = channel->offermsg.child_relid;
397 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700398
Hank Janssen3e7ee492009-07-13 16:02:34 -0700399
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800400 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800401 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800402 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700403
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800404 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700405
Haiyang Zhangc6977672011-01-26 12:12:08 -0800406 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700407 sizeof(*msginfo));
Hank Janssen98e08702011-03-29 13:58:44 -0700408 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700409 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700410
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700411 if (msgcount > 1) {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800412 list_for_each(curr, &msginfo->submsglist) {
Bill Pemberton53af5452009-09-11 21:46:44 -0400413
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700414 submsginfo = (struct vmbus_channel_msginfo *)curr;
415 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800416 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700417
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800418 gpadl_body->header.msgtype =
419 CHANNELMSG_GPADL_BODY;
420 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700421
Haiyang Zhangc6977672011-01-26 12:12:08 -0800422 ret = vmbus_post_msg(gpadl_body,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800423 submsginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700424 sizeof(*submsginfo));
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000425 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700426 goto cleanup;
Bill Pemberton99259152010-05-05 15:27:36 -0400427
Hank Janssen3e7ee492009-07-13 16:02:34 -0700428 }
429 }
K. Y. Srinivasan72c6b712014-08-27 16:25:34 -0700430 wait_for_completion(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700431
Bill Pemberton454f18a2009-07-27 16:47:24 -0400432 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800433 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700434
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700435cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800436 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800437 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800438 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700439
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700440 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700441 return ret;
442}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700443EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700444
Hank Janssen3e189512010-03-04 22:11:00 +0000445/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700446 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700447 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700448int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700449{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700450 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700451 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700452 unsigned long flags;
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700453 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700454
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700455 info = kmalloc(sizeof(*info) +
456 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400457 if (!info)
458 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700459
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700460 init_completion(&info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700461
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800462 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700463
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800464 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
465 msg->child_relid = channel->offermsg.child_relid;
466 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700467
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800468 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800469 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800470 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800471 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc6977672011-01-26 12:12:08 -0800472 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700473 sizeof(struct vmbus_channel_gpadl_teardown));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700474
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700475 if (ret)
476 goto post_msg_err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700477
K. Y. Srinivasan66be6532014-08-27 16:25:32 -0700478 wait_for_completion(&info->waitevent);
479
480post_msg_err:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800481 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800482 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800483 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700484
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700485 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700486 return ret;
487}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700488EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700489
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700490static void reset_channel_cb(void *arg)
491{
492 struct vmbus_channel *channel = arg;
493
494 channel->onchannel_callback = NULL;
495}
496
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700497static int vmbus_close_internal(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700498{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700499 struct vmbus_channel_close_channel *msg;
Dexuan Cui63d55b22015-12-14 16:01:47 -0800500 struct tasklet_struct *tasklet;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700501 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700502
Dexuan Cui63d55b22015-12-14 16:01:47 -0800503 /*
504 * process_chn_event(), running in the tasklet, can race
505 * with vmbus_close_internal() in the case of SMP guest, e.g., when
506 * the former is accessing channel->inbound.ring_buffer, the latter
507 * could be freeing the ring_buffer pages.
508 *
509 * To resolve the race, we can serialize them by disabling the
510 * tasklet when the latter is running here.
511 */
512 tasklet = hv_context.event_dpc[channel->target_cpu];
513 tasklet_disable(tasklet);
514
Dexuan Cui64b7faf2015-12-14 16:01:48 -0800515 /*
516 * In case a device driver's probe() fails (e.g.,
517 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
518 * rescinded later (e.g., we dynamically disble an Integrated Service
519 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
520 * here we should skip most of the below cleanup work.
521 */
522 if (channel->state != CHANNEL_OPENED_STATE) {
523 ret = -EINVAL;
524 goto out;
525 }
526
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700527 channel->state = CHANNEL_OPEN_STATE;
528 channel->sc_creation_callback = NULL;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400529 /* Stop callback and cancel the timer asap */
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700530 if (channel->target_cpu != get_cpu()) {
531 put_cpu();
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700532 smp_call_function_single(channel->target_cpu, reset_channel_cb,
533 channel, true);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700534 } else {
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700535 reset_channel_cb(channel);
K. Y. Srinivasan2115b562014-08-28 18:29:53 -0700536 put_cpu();
537 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700538
Bill Pemberton454f18a2009-07-27 16:47:24 -0400539 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700540
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700541 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700542
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800543 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
544 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700545
Haiyang Zhangc6977672011-01-26 12:12:08 -0800546 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700547
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700548 if (ret) {
549 pr_err("Close failed: close post msg return is %d\n", ret);
550 /*
551 * If we failed to post the close msg,
552 * it is perhaps better to leak memory.
553 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800554 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700555 }
556
Bill Pemberton454f18a2009-07-27 16:47:24 -0400557 /* Tear down the gpadl for the channel's ring buffer */
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700558 if (channel->ringbuffer_gpadlhandle) {
559 ret = vmbus_teardown_gpadl(channel,
560 channel->ringbuffer_gpadlhandle);
561 if (ret) {
562 pr_err("Close failed: teardown gpadl return %d\n", ret);
563 /*
564 * If we failed to teardown gpadl,
565 * it is perhaps better to leak memory.
566 */
Dexuan Cui63d55b22015-12-14 16:01:47 -0800567 goto out;
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700568 }
569 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700570
Bill Pemberton454f18a2009-07-27 16:47:24 -0400571 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700572 hv_ringbuffer_cleanup(&channel->outbound);
573 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700574
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800575 free_pages((unsigned long)channel->ringbuffer_pages,
576 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700577
Dexuan Cui63d55b22015-12-14 16:01:47 -0800578out:
579 tasklet_enable(tasklet);
580
K. Y. Srinivasan98d731b2014-08-27 16:25:33 -0700581 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700582}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700583
584/*
585 * vmbus_close - Close the specified channel
586 */
587void vmbus_close(struct vmbus_channel *channel)
588{
589 struct list_head *cur, *tmp;
590 struct vmbus_channel *cur_channel;
591
592 if (channel->primary_channel != NULL) {
593 /*
594 * We will only close sub-channels when
595 * the primary is closed.
596 */
597 return;
598 }
599 /*
600 * Close all the sub-channels first and then close the
601 * primary channel.
602 */
603 list_for_each_safe(cur, tmp, &channel->sc_list) {
604 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
605 if (cur_channel->state != CHANNEL_OPENED_STATE)
606 continue;
607 vmbus_close_internal(cur_channel);
608 }
609 /*
610 * Now close the primary.
611 */
612 vmbus_close_internal(channel);
613}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700614EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700615
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800616int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700617 u32 bufferlen, u64 requestid,
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800618 enum vmbus_packet_type type, u32 flags, bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700619{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700620 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700621 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100622 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800623 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700624 u64 aligned_data = 0;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700625 int ret;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800626 bool signal = false;
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700627 int num_vecs = ((bufferlen != 0) ? 3 : 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700628
Hank Janssen3e7ee492009-07-13 16:02:34 -0700629
Bill Pemberton454f18a2009-07-27 16:47:24 -0400630 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800631 desc.type = type; /* VmbusPacketTypeDataInBand; */
632 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700633 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800634 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
635 desc.len8 = (u16)(packetlen_aligned >> 3);
636 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700637
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800638 bufferlist[0].iov_base = &desc;
639 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
640 bufferlist[1].iov_base = buffer;
641 bufferlist[1].iov_len = bufferlen;
642 bufferlist[2].iov_base = &aligned_data;
643 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700644
K. Y. Srinivasanb81658c2015-08-01 16:08:14 -0700645 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, num_vecs,
646 &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700647
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700648 /*
649 * Signalling the host is conditional on many factors:
650 * 1. The ring state changed from being empty to non-empty.
651 * This is tracked by the variable "signal".
652 * 2. The variable kick_q tracks if more data will be placed
653 * on the ring. We will not signal if more data is
654 * to be placed.
655 *
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800656 * Based on the channel signal state, we will decide
657 * which signaling policy will be applied.
658 *
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700659 * If we cannot write to the ring-buffer; signal the host
660 * even if we may not have written anything. This is a rare
661 * enough condition that it should not matter.
662 */
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800663
664 if (channel->signal_policy)
665 signal = true;
666 else
667 kick_q = true;
668
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700669 if (((ret == 0) && kick_q && signal) || (ret))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700670 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700671
Hank Janssen3e7ee492009-07-13 16:02:34 -0700672 return ret;
673}
K. Y. Srinivasane9395e32015-02-28 11:39:04 -0800674EXPORT_SYMBOL(vmbus_sendpacket_ctl);
675
676/**
677 * vmbus_sendpacket() - Send the specified buffer on the given channel
678 * @channel: Pointer to vmbus_channel structure.
679 * @buffer: Pointer to the buffer you want to receive the data into.
680 * @bufferlen: Maximum size of what the the buffer will hold
681 * @requestid: Identifier of the request
682 * @type: Type of packet that is being send e.g. negotiate, time
683 * packet etc.
684 *
685 * Sends data in @buffer directly to hyper-v via the vmbus
686 * This will send the data unparsed to hyper-v.
687 *
688 * Mainly used by Hyper-V drivers.
689 */
690int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
691 u32 bufferlen, u64 requestid,
692 enum vmbus_packet_type type, u32 flags)
693{
694 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
695 type, flags, true);
696}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700697EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700698
Hank Janssen3e189512010-03-04 22:11:00 +0000699/*
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800700 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
701 * packets using a GPADL Direct packet type. This interface allows you
702 * to control notifying the host. This will be useful for sending
703 * batched data. Also the sender can control the send flags
704 * explicitly.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700705 */
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800706int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700707 struct hv_page_buffer pagebuffers[],
708 u32 pagecount, void *buffer, u32 bufferlen,
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800709 u64 requestid,
710 u32 flags,
711 bool kick_q)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700712{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700713 int ret;
714 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000715 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700716 u32 descsize;
717 u32 packetlen;
718 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800719 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700720 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800721 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700722
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700723 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400724 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700725
Hank Janssen3e7ee492009-07-13 16:02:34 -0700726
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700727 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000728 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700729 * largest size we support
730 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700731 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
732 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700733 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700734 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100735 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700736
Bill Pemberton454f18a2009-07-27 16:47:24 -0400737 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800738 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800739 desc.flags = flags;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700740 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
741 desc.length8 = (u16)(packetlen_aligned >> 3);
742 desc.transactionid = requestid;
743 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700744
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700745 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800746 desc.range[i].len = pagebuffers[i].len;
747 desc.range[i].offset = pagebuffers[i].offset;
748 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700749 }
750
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800751 bufferlist[0].iov_base = &desc;
752 bufferlist[0].iov_len = descsize;
753 bufferlist[1].iov_base = buffer;
754 bufferlist[1].iov_len = bufferlen;
755 bufferlist[2].iov_base = &aligned_data;
756 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700757
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800758 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700759
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700760 /*
761 * Signalling the host is conditional on many factors:
762 * 1. The ring state changed from being empty to non-empty.
763 * This is tracked by the variable "signal".
764 * 2. The variable kick_q tracks if more data will be placed
765 * on the ring. We will not signal if more data is
766 * to be placed.
767 *
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800768 * Based on the channel signal state, we will decide
769 * which signaling policy will be applied.
770 *
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700771 * If we cannot write to the ring-buffer; signal the host
772 * even if we may not have written anything. This is a rare
773 * enough condition that it should not matter.
774 */
K. Y. Srinivasan85998462015-12-14 16:01:54 -0800775
776 if (channel->signal_policy)
777 signal = true;
778 else
779 kick_q = true;
780
K. Y. Srinivasan5f5cc812015-03-18 12:29:30 -0700781 if (((ret == 0) && kick_q && signal) || (ret))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700782 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700783
Hank Janssen3e7ee492009-07-13 16:02:34 -0700784 return ret;
785}
K. Y. Srinivasanb3a19b32015-03-18 12:29:29 -0700786EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
K. Y. Srinivasan87e93d62015-02-28 11:39:03 -0800787
788/*
789 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
790 * packets using a GPADL Direct packet type.
791 */
792int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
793 struct hv_page_buffer pagebuffers[],
794 u32 pagecount, void *buffer, u32 bufferlen,
795 u64 requestid)
796{
797 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
798 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
799 buffer, bufferlen, requestid,
800 flags, true);
801
802}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700803EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700804
Hank Janssen3e189512010-03-04 22:11:00 +0000805/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700806 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000807 * using a GPADL Direct packet type.
K. Y. Srinivasand61031e2015-01-09 23:54:34 -0800808 * The buffer includes the vmbus descriptor.
809 */
810int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
811 struct vmbus_packet_mpb_array *desc,
812 u32 desc_size,
813 void *buffer, u32 bufferlen, u64 requestid)
814{
815 int ret;
816 u32 packetlen;
817 u32 packetlen_aligned;
818 struct kvec bufferlist[3];
819 u64 aligned_data = 0;
820 bool signal = false;
821
822 packetlen = desc_size + bufferlen;
823 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
824
825 /* Setup the descriptor */
826 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
827 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
828 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
829 desc->length8 = (u16)(packetlen_aligned >> 3);
830 desc->transactionid = requestid;
831 desc->rangecount = 1;
832
833 bufferlist[0].iov_base = desc;
834 bufferlist[0].iov_len = desc_size;
835 bufferlist[1].iov_base = buffer;
836 bufferlist[1].iov_len = bufferlen;
837 bufferlist[2].iov_base = &aligned_data;
838 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
839
840 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
841
842 if (ret == 0 && signal)
843 vmbus_setevent(channel);
844
845 return ret;
846}
847EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
848
849/*
850 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
851 * using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700852 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700853int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700854 struct hv_multipage_buffer *multi_pagebuffer,
855 void *buffer, u32 bufferlen, u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700856{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700857 int ret;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000858 struct vmbus_channel_packet_multipage_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700859 u32 descsize;
860 u32 packetlen;
861 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800862 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700863 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800864 bool signal = false;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800865 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
866 multi_pagebuffer->len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700867
Tobias Klauser24b8a402014-04-25 17:03:50 +0200868 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400869 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700870
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700871 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000872 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700873 * the largest size we support
874 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700875 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
876 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700877 sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700878 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100879 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700880
Hank Janssen3e7ee492009-07-13 16:02:34 -0700881
Bill Pemberton454f18a2009-07-27 16:47:24 -0400882 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800883 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000884 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700885 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
886 desc.length8 = (u16)(packetlen_aligned >> 3);
887 desc.transactionid = requestid;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000888 desc.rangecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700889
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800890 desc.range.len = multi_pagebuffer->len;
891 desc.range.offset = multi_pagebuffer->offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700892
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800893 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700894 pfncount * sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700895
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800896 bufferlist[0].iov_base = &desc;
897 bufferlist[0].iov_len = descsize;
898 bufferlist[1].iov_base = buffer;
899 bufferlist[1].iov_len = bufferlen;
900 bufferlist[2].iov_base = &aligned_data;
901 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700902
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800903 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700904
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800905 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700906 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700907
Hank Janssen3e7ee492009-07-13 16:02:34 -0700908 return ret;
909}
Greg Kroah-Hartman4cb106f2010-10-21 09:23:59 -0700910EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700911
912/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700913 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700914 * @channel: Pointer to vmbus_channel structure.
915 * @buffer: Pointer to the buffer you want to receive the data into.
916 * @bufferlen: Maximum size of what the the buffer will hold
917 * @buffer_actual_len: The actual size of the data after it was received
918 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700919 *
920 * Receives directly from the hyper-v vmbus and puts the data it received
921 * into Buffer. This will receive the data unparsed from hyper-v.
922 *
923 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700924 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700925int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700926 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700927{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700928 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700929 u32 packetlen;
930 u32 userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700931 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800932 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700933
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700934 *buffer_actual_len = 0;
935 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700936
Hank Janssen3e7ee492009-07-13 16:02:34 -0700937
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700938 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700939 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700940 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700941 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700942
Haiyang Zhang415f2282011-01-26 12:12:13 -0800943 packetlen = desc.len8 << 3;
944 userlen = packetlen - (desc.offset8 << 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700945
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700946 *buffer_actual_len = userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700947
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700948 if (userlen > bufferlen) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700949
Hank Janssen0a466182011-03-29 13:58:47 -0700950 pr_err("Buffer too small - got %d needs %d\n",
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700951 bufferlen, userlen);
K. Y. Srinivasan926ae5262011-06-06 15:50:11 -0700952 return -ETOOSMALL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700953 }
954
Haiyang Zhang415f2282011-01-26 12:12:13 -0800955 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700956
Bill Pemberton454f18a2009-07-27 16:47:24 -0400957 /* Copy over the packet to the user buffer */
K. Y. Srinivasan38397c82011-05-10 07:55:25 -0700958 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800959 (desc.offset8 << 3), &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700960
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800961 if (signal)
962 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700963
Hank Janssen3e7ee492009-07-13 16:02:34 -0700964 return 0;
965}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700966EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700967
Hank Janssen3e189512010-03-04 22:11:00 +0000968/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700969 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700970 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700971int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700972 u32 bufferlen, u32 *buffer_actual_len,
973 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700974{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700975 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700976 u32 packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700977 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800978 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700979
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700980 *buffer_actual_len = 0;
981 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700982
Hank Janssen3e7ee492009-07-13 16:02:34 -0700983
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700984 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700985 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700986 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700987 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700988
Hank Janssen3e7ee492009-07-13 16:02:34 -0700989
Haiyang Zhang415f2282011-01-26 12:12:13 -0800990 packetlen = desc.len8 << 3;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700991
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700992 *buffer_actual_len = packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700993
Jason Wang7a446d62014-06-30 14:14:48 +0800994 if (packetlen > bufferlen)
K. Y. Srinivasan3d5cad92011-08-25 09:48:59 -0700995 return -ENOBUFS;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700996
Haiyang Zhang415f2282011-01-26 12:12:13 -0800997 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700998
Bill Pemberton454f18a2009-07-27 16:47:24 -0400999 /* Copy over the entire packet to the user buffer */
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -08001000 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
1001 &signal);
1002
1003 if (signal)
1004 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001005
Dan Carpenter33b06932013-09-17 00:58:36 +03001006 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001007}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -07001008EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);