blob: 6de6c98ce6eb73636d06123024e9f84324947f59 [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. Srinivasan3f335ea2011-05-12 19:34:15 -070030
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070031#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070032
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080033#define NUM_PAGES_SPANNED(addr, len) \
34((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
35
Hank Janssen3e189512010-03-04 22:11:00 +000036/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070037 * vmbus_setevent- Trigger an event notification on the specified
Hank Janssen3e189512010-03-04 22:11:00 +000038 * channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070039 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070040static void vmbus_setevent(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -070041{
Haiyang Zhang39d70a42010-09-30 10:52:13 -070042 struct hv_monitor_page *monitorpage;
Hank Janssen3e7ee492009-07-13 16:02:34 -070043
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080044 if (channel->offermsg.monitor_allocated) {
Bill Pemberton454f18a2009-07-27 16:47:24 -040045 /* Each u32 represents 32 channels */
Olaf Hering22356582011-03-21 14:41:37 +010046 sync_set_bit(channel->offermsg.child_relid & 31,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080047 (unsigned long *) vmbus_connection.send_int_page +
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080048 (channel->offermsg.child_relid >> 5));
Hank Janssen3e7ee492009-07-13 16:02:34 -070049
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080050 monitorpage = vmbus_connection.monitor_pages;
Haiyang Zhang39d70a42010-09-30 10:52:13 -070051 monitorpage++; /* Get the child to parent monitor page */
Hank Janssen3e7ee492009-07-13 16:02:34 -070052
Olaf Hering22356582011-03-21 14:41:37 +010053 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080054 (unsigned long *)&monitorpage->trigger_group
55 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -040056
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070057 } else {
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -080058 vmbus_set_event(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -070059 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070060}
61
Hank Janssen3e189512010-03-04 22:11:00 +000062/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070063 * vmbus_get_debug_info -Retrieve various channel debug info
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070064 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070065void vmbus_get_debug_info(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -070066 struct vmbus_channel_debug_info *debuginfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -070067{
Haiyang Zhang39d70a42010-09-30 10:52:13 -070068 struct hv_monitor_page *monitorpage;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080069 u8 monitor_group = (u8)channel->offermsg.monitorid / 32;
70 u8 monitor_offset = (u8)channel->offermsg.monitorid % 32;
Hank Janssen3e7ee492009-07-13 16:02:34 -070071
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080072 debuginfo->relid = channel->offermsg.child_relid;
73 debuginfo->state = channel->state;
74 memcpy(&debuginfo->interfacetype,
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -070075 &channel->offermsg.offer.if_type, sizeof(uuid_le));
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080076 memcpy(&debuginfo->interface_instance,
Haiyang Zhang767dff62011-01-26 12:12:12 -080077 &channel->offermsg.offer.if_instance,
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -070078 sizeof(uuid_le));
Hank Janssen3e7ee492009-07-13 16:02:34 -070079
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080080 monitorpage = (struct hv_monitor_page *)vmbus_connection.monitor_pages;
Hank Janssen3e7ee492009-07-13 16:02:34 -070081
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080082 debuginfo->monitorid = channel->offermsg.monitorid;
Hank Janssen3e7ee492009-07-13 16:02:34 -070083
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080084 debuginfo->servermonitor_pending =
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080085 monitorpage->trigger_group[monitor_group].pending;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080086 debuginfo->servermonitor_latency =
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080087 monitorpage->latency[monitor_group][monitor_offset];
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080088 debuginfo->servermonitor_connectionid =
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080089 monitorpage->parameter[monitor_group]
90 [monitor_offset].connectionid.u.id;
Hank Janssen3e7ee492009-07-13 16:02:34 -070091
Haiyang Zhang39d70a42010-09-30 10:52:13 -070092 monitorpage++;
Hank Janssen3e7ee492009-07-13 16:02:34 -070093
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080094 debuginfo->clientmonitor_pending =
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080095 monitorpage->trigger_group[monitor_group].pending;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080096 debuginfo->clientmonitor_latency =
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080097 monitorpage->latency[monitor_group][monitor_offset];
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080098 debuginfo->clientmonitor_connectionid =
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080099 monitorpage->parameter[monitor_group]
100 [monitor_offset].connectionid.u.id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700101
K. Y. Srinivasana75b61d2011-05-10 07:55:28 -0700102 hv_ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound);
103 hv_ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700104}
105
Hank Janssen3e189512010-03-04 22:11:00 +0000106/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700107 * vmbus_open - Open the specified channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700108 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700109int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700110 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
111 void (*onchannelcallback)(void *context), void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700112{
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700113 struct vmbus_channel_open_channel *open_msg;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700114 struct vmbus_channel_msginfo *open_info = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700115 void *in, *out;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700116 unsigned long flags;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700117 int ret, t, err = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700118
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700119 spin_lock_irqsave(&newchannel->sc_lock, flags);
120 if (newchannel->state == CHANNEL_OPEN_STATE) {
121 newchannel->state = CHANNEL_OPENING_STATE;
122 } else {
123 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
124 return -EINVAL;
125 }
126 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
127
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800128 newchannel->onchannel_callback = onchannelcallback;
129 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700130
Bill Pemberton454f18a2009-07-27 16:47:24 -0400131 /* Allocate the ring buffer */
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800132 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
133 get_order(send_ringbuffer_size + recv_ringbuffer_size));
134
Bill Pemberton7e052d92010-05-05 15:27:30 -0400135 if (!out)
136 return -ENOMEM;
137
Hank Janssen3e7ee492009-07-13 16:02:34 -0700138
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700139 in = (void *)((unsigned long)out + send_ringbuffer_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700140
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800141 newchannel->ringbuffer_pages = out;
142 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700143 recv_ringbuffer_size) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700144
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700145 ret = hv_ringbuffer_init(
146 &newchannel->outbound, out, send_ringbuffer_size);
147
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000148 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400149 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700150 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400151 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700152
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700153 ret = hv_ringbuffer_init(
154 &newchannel->inbound, in, recv_ringbuffer_size);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000155 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400156 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700157 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400158 }
159
Hank Janssen3e7ee492009-07-13 16:02:34 -0700160
Bill Pemberton454f18a2009-07-27 16:47:24 -0400161 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800162 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700163
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700164 ret = vmbus_establish_gpadl(newchannel,
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800165 newchannel->outbound.ring_buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700166 send_ringbuffer_size +
167 recv_ringbuffer_size,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800168 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400169
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000170 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400171 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700172 goto error0;
Bill Pembertonb94ef342010-05-05 15:27:37 -0400173 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700174
Bill Pemberton454f18a2009-07-27 16:47:24 -0400175 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700176 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700177 sizeof(struct vmbus_channel_open_channel),
178 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700179 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400180 err = -ENOMEM;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700181 goto error0;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400182 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700183
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700184 init_completion(&open_info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700185
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700186 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700187 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
188 open_msg->openid = newchannel->offermsg.child_relid;
189 open_msg->child_relid = newchannel->offermsg.child_relid;
190 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
191 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700192 PAGE_SHIFT;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800193 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700194
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700195 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400196 err = -EINVAL;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700197 goto error0;
Bill Pembertonc827f942010-05-05 15:27:38 -0400198 }
199
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700200 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700201 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700202
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800203 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700204 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800205 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800206 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700207
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700208 ret = vmbus_post_msg(open_msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700209 sizeof(struct vmbus_channel_open_channel));
Hank Janssen98e08702011-03-29 13:58:44 -0700210
211 if (ret != 0)
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700212 goto error1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700213
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700214 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700215 if (t == 0) {
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800216 err = -ETIMEDOUT;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700217 goto error1;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800218 }
219
Hank Janssen3e7ee492009-07-13 16:02:34 -0700220
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700221 if (open_info->response.open_result.status)
222 err = open_info->response.open_result.status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700223
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800224 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700225 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800226 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700227
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700228 if (err == 0)
229 newchannel->state = CHANNEL_OPENED_STATE;
230
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700231 kfree(open_info);
Hank Janssen98e08702011-03-29 13:58:44 -0700232 return err;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400233
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700234error1:
235 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
236 list_del(&open_info->msglistentry);
237 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
238
239error0:
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800240 free_pages((unsigned long)out,
241 get_order(send_ringbuffer_size + recv_ringbuffer_size));
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700242 kfree(open_info);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400243 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700244}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700245EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700246
Hank Janssen3e189512010-03-04 22:11:00 +0000247/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700248 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700249 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700250static int create_gpadl_header(void *kbuffer, u32 size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700251 struct vmbus_channel_msginfo **msginfo,
252 u32 *messagecount)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700253{
254 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700255 int pagecount;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700256 unsigned long long pfn;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700257 struct vmbus_channel_gpadl_header *gpadl_header;
258 struct vmbus_channel_gpadl_body *gpadl_body;
259 struct vmbus_channel_msginfo *msgheader;
260 struct vmbus_channel_msginfo *msgbody = NULL;
261 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700262
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700263 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700264
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700265 pagecount = size >> PAGE_SHIFT;
266 pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700267
Bill Pemberton454f18a2009-07-27 16:47:24 -0400268 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700269 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700270 sizeof(struct vmbus_channel_gpadl_header) -
271 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700272 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700273
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700274 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700275 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400276 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700277 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700278 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700279 sizeof(struct gpa_range) + pfncount * sizeof(u64);
280 msgheader = kzalloc(msgsize, GFP_KERNEL);
281 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400282 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700283
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800284 INIT_LIST_HEAD(&msgheader->submsglist);
285 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700286
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700287 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800288 msgheader->msg;
289 gpadl_header->rangecount = 1;
290 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700291 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800292 gpadl_header->range[0].byte_offset = 0;
293 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700294 for (i = 0; i < pfncount; i++)
Haiyang Zhang415f2282011-01-26 12:12:13 -0800295 gpadl_header->range[0].pfn_array[i] = pfn+i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700296 *msginfo = msgheader;
297 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700298
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700299 pfnsum = pfncount;
300 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700301
Bill Pemberton454f18a2009-07-27 16:47:24 -0400302 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700303 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700304 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700305 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700306
Bill Pemberton454f18a2009-07-27 16:47:24 -0400307 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700308 while (pfnleft) {
309 if (pfnleft > pfncount)
310 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700311 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700312 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700313
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700314 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700315 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700316 pfncurr * sizeof(u64);
317 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700318
319 if (!msgbody) {
320 struct vmbus_channel_msginfo *pos = NULL;
321 struct vmbus_channel_msginfo *tmp = NULL;
322 /*
323 * Free up all the allocated messages.
324 */
325 list_for_each_entry_safe(pos, tmp,
326 &msgheader->submsglist,
327 msglistentry) {
328
329 list_del(&pos->msglistentry);
330 kfree(pos);
331 }
332
Bill Pembertond1c250b2010-05-05 15:27:35 -0400333 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700334 }
335
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800336 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700337 (*messagecount)++;
338 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800339 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700340
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700341 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700342 * Gpadl is u32 and we are using a pointer which could
343 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700344 * This is governed by the guest/host protocol and
345 * so the hypervisor gurantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700346 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700347 for (i = 0; i < pfncurr; i++)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800348 gpadl_body->pfn[i] = pfn + pfnsum + i;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700349
Bill Pemberton454f18a2009-07-27 16:47:24 -0400350 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800351 list_add_tail(&msgbody->msglistentry,
352 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700353 pfnsum += pfncurr;
354 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700355 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700356 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400357 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700358 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700359 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700360 sizeof(struct gpa_range) + pagecount * sizeof(u64);
361 msgheader = kzalloc(msgsize, GFP_KERNEL);
362 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400363 goto nomem;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800364 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700365
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700366 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800367 msgheader->msg;
368 gpadl_header->rangecount = 1;
369 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700370 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800371 gpadl_header->range[0].byte_offset = 0;
372 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700373 for (i = 0; i < pagecount; i++)
Haiyang Zhang415f2282011-01-26 12:12:13 -0800374 gpadl_header->range[0].pfn_array[i] = pfn+i;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700375
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700376 *msginfo = msgheader;
377 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700378 }
379
380 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400381nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700382 kfree(msgheader);
383 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400384 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700385}
386
Hank Janssen3e189512010-03-04 22:11:00 +0000387/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700388 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700389 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700390 * @channel: a channel
391 * @kbuffer: from kmalloc
392 * @size: page-size multiple
393 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700394 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700395int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700396 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700397{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700398 struct vmbus_channel_gpadl_header *gpadlmsg;
399 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700400 struct vmbus_channel_msginfo *msginfo = NULL;
401 struct vmbus_channel_msginfo *submsginfo;
402 u32 msgcount;
Bill Pemberton53af5452009-09-11 21:46:44 -0400403 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700404 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700405 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400406 int ret = 0;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700407 int t;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700408
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800409 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
410 atomic_inc(&vmbus_connection.next_gpadl_handle);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700411
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700412 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400413 if (ret)
414 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700415
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700416 init_completion(&msginfo->waitevent);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400417
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800418 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
419 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
420 gpadlmsg->child_relid = channel->offermsg.child_relid;
421 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700422
Hank Janssen3e7ee492009-07-13 16:02:34 -0700423
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800424 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800425 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800426 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700427
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800428 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700429
Haiyang Zhangc6977672011-01-26 12:12:08 -0800430 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700431 sizeof(*msginfo));
Hank Janssen98e08702011-03-29 13:58:44 -0700432 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700433 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700434
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700435 if (msgcount > 1) {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800436 list_for_each(curr, &msginfo->submsglist) {
Bill Pemberton53af5452009-09-11 21:46:44 -0400437
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700438 submsginfo = (struct vmbus_channel_msginfo *)curr;
439 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800440 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700441
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800442 gpadl_body->header.msgtype =
443 CHANNELMSG_GPADL_BODY;
444 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700445
Haiyang Zhangc6977672011-01-26 12:12:08 -0800446 ret = vmbus_post_msg(gpadl_body,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800447 submsginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700448 sizeof(*submsginfo));
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000449 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700450 goto cleanup;
Bill Pemberton99259152010-05-05 15:27:36 -0400451
Hank Janssen3e7ee492009-07-13 16:02:34 -0700452 }
453 }
K. Y. Srinivasan40961de2011-06-06 15:50:05 -0700454 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700455 BUG_ON(t == 0);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800456
Hank Janssen3e7ee492009-07-13 16:02:34 -0700457
Bill Pemberton454f18a2009-07-27 16:47:24 -0400458 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800459 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700460
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700461cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800462 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800463 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800464 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700465
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700466 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700467 return ret;
468}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700469EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700470
Hank Janssen3e189512010-03-04 22:11:00 +0000471/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700472 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700473 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700474int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700475{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700476 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700477 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700478 unsigned long flags;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700479 int ret, t;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700480
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700481 info = kmalloc(sizeof(*info) +
482 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400483 if (!info)
484 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700485
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700486 init_completion(&info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700487
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800488 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700489
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800490 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
491 msg->child_relid = channel->offermsg.child_relid;
492 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700493
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800494 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800495 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800496 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800497 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc6977672011-01-26 12:12:08 -0800498 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700499 sizeof(struct vmbus_channel_gpadl_teardown));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700500
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800501 BUG_ON(ret != 0);
K. Y. Srinivasan40961de2011-06-06 15:50:05 -0700502 t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700503 BUG_ON(t == 0);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700504
Bill Pemberton454f18a2009-07-27 16:47:24 -0400505 /* Received a torndown response */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800506 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800507 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800508 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700509
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700510 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700511 return ret;
512}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700513EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700514
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700515static void vmbus_close_internal(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700516{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700517 struct vmbus_channel_close_channel *msg;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700518 int ret;
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700519 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700520
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700521 channel->state = CHANNEL_OPEN_STATE;
522 channel->sc_creation_callback = NULL;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400523 /* Stop callback and cancel the timer asap */
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700524 spin_lock_irqsave(&channel->inbound_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800525 channel->onchannel_callback = NULL;
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700526 spin_unlock_irqrestore(&channel->inbound_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700527
Bill Pemberton454f18a2009-07-27 16:47:24 -0400528 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700529
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700530 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700531
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800532 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
533 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700534
Haiyang Zhangc6977672011-01-26 12:12:08 -0800535 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700536
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800537 BUG_ON(ret != 0);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400538 /* Tear down the gpadl for the channel's ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800539 if (channel->ringbuffer_gpadlhandle)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700540 vmbus_teardown_gpadl(channel,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800541 channel->ringbuffer_gpadlhandle);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700542
Bill Pemberton454f18a2009-07-27 16:47:24 -0400543 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700544 hv_ringbuffer_cleanup(&channel->outbound);
545 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700546
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800547 free_pages((unsigned long)channel->ringbuffer_pages,
548 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700549
Hank Janssen3e7ee492009-07-13 16:02:34 -0700550
Hank Janssen3e7ee492009-07-13 16:02:34 -0700551}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700552
553/*
554 * vmbus_close - Close the specified channel
555 */
556void vmbus_close(struct vmbus_channel *channel)
557{
558 struct list_head *cur, *tmp;
559 struct vmbus_channel *cur_channel;
560
561 if (channel->primary_channel != NULL) {
562 /*
563 * We will only close sub-channels when
564 * the primary is closed.
565 */
566 return;
567 }
568 /*
569 * Close all the sub-channels first and then close the
570 * primary channel.
571 */
572 list_for_each_safe(cur, tmp, &channel->sc_list) {
573 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
574 if (cur_channel->state != CHANNEL_OPENED_STATE)
575 continue;
576 vmbus_close_internal(cur_channel);
577 }
578 /*
579 * Now close the primary.
580 */
581 vmbus_close_internal(channel);
582}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700583EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700584
Hank Janssenc88c4e42010-05-04 15:55:05 -0700585/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700586 * vmbus_sendpacket() - Send the specified buffer on the given channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700587 * @channel: Pointer to vmbus_channel structure.
588 * @buffer: Pointer to the buffer you want to receive the data into.
589 * @bufferlen: Maximum size of what the the buffer will hold
590 * @requestid: Identifier of the request
591 * @type: Type of packet that is being send e.g. negotiate, time
Hank Janssenc88c4e42010-05-04 15:55:05 -0700592 * packet etc.
593 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700594 * Sends data in @buffer directly to hyper-v via the vmbus
Hank Janssenc88c4e42010-05-04 15:55:05 -0700595 * This will send the data unparsed to hyper-v.
596 *
597 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700598 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700599int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700600 u32 bufferlen, u64 requestid,
601 enum vmbus_packet_type type, u32 flags)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700602{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700603 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700604 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100605 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700606 struct scatterlist bufferlist[3];
607 u64 aligned_data = 0;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700608 int ret;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800609 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700610
Hank Janssen3e7ee492009-07-13 16:02:34 -0700611
Bill Pemberton454f18a2009-07-27 16:47:24 -0400612 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800613 desc.type = type; /* VmbusPacketTypeDataInBand; */
614 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700615 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800616 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
617 desc.len8 = (u16)(packetlen_aligned >> 3);
618 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700619
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700620 sg_init_table(bufferlist, 3);
621 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
622 sg_set_buf(&bufferlist[1], buffer, bufferlen);
623 sg_set_buf(&bufferlist[2], &aligned_data,
624 packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700625
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800626 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700627
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800628 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700629 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700630
Hank Janssen3e7ee492009-07-13 16:02:34 -0700631 return ret;
632}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700633EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700634
Hank Janssen3e189512010-03-04 22:11:00 +0000635/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700636 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
Hank Janssen3e189512010-03-04 22:11:00 +0000637 * packets using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700638 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700639int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700640 struct hv_page_buffer pagebuffers[],
641 u32 pagecount, void *buffer, u32 bufferlen,
642 u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700643{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700644 int ret;
645 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000646 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700647 u32 descsize;
648 u32 packetlen;
649 u32 packetlen_aligned;
650 struct scatterlist bufferlist[3];
651 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800652 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700653
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700654 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400655 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700656
Hank Janssen3e7ee492009-07-13 16:02:34 -0700657
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700658 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000659 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700660 * largest size we support
661 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700662 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
663 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700664 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700665 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100666 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700667
Bill Pemberton454f18a2009-07-27 16:47:24 -0400668 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800669 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000670 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700671 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
672 desc.length8 = (u16)(packetlen_aligned >> 3);
673 desc.transactionid = requestid;
674 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700675
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700676 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800677 desc.range[i].len = pagebuffers[i].len;
678 desc.range[i].offset = pagebuffers[i].offset;
679 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700680 }
681
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700682 sg_init_table(bufferlist, 3);
683 sg_set_buf(&bufferlist[0], &desc, descsize);
684 sg_set_buf(&bufferlist[1], buffer, bufferlen);
685 sg_set_buf(&bufferlist[2], &aligned_data,
686 packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700687
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800688 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700689
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800690 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700691 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700692
Hank Janssen3e7ee492009-07-13 16:02:34 -0700693 return ret;
694}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700695EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700696
Hank Janssen3e189512010-03-04 22:11:00 +0000697/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700698 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000699 * using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700700 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700701int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700702 struct hv_multipage_buffer *multi_pagebuffer,
703 void *buffer, u32 bufferlen, u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700704{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700705 int ret;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000706 struct vmbus_channel_packet_multipage_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700707 u32 descsize;
708 u32 packetlen;
709 u32 packetlen_aligned;
710 struct scatterlist bufferlist[3];
711 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800712 bool signal = false;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800713 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
714 multi_pagebuffer->len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700715
Hank Janssen3e7ee492009-07-13 16:02:34 -0700716
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700717 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
Bill Pemberton002b53e2010-05-05 15:27:39 -0400718 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700719
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700720 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000721 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700722 * the largest size we support
723 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700724 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
725 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700726 sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700727 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100728 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700729
Hank Janssen3e7ee492009-07-13 16:02:34 -0700730
Bill Pemberton454f18a2009-07-27 16:47:24 -0400731 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800732 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000733 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700734 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
735 desc.length8 = (u16)(packetlen_aligned >> 3);
736 desc.transactionid = requestid;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000737 desc.rangecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700738
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800739 desc.range.len = multi_pagebuffer->len;
740 desc.range.offset = multi_pagebuffer->offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700741
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800742 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700743 pfncount * sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700744
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700745 sg_init_table(bufferlist, 3);
746 sg_set_buf(&bufferlist[0], &desc, descsize);
747 sg_set_buf(&bufferlist[1], buffer, bufferlen);
748 sg_set_buf(&bufferlist[2], &aligned_data,
749 packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700750
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800751 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700752
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800753 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700754 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700755
Hank Janssen3e7ee492009-07-13 16:02:34 -0700756 return ret;
757}
Greg Kroah-Hartman4cb106f2010-10-21 09:23:59 -0700758EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700759
760/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700761 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700762 * @channel: Pointer to vmbus_channel structure.
763 * @buffer: Pointer to the buffer you want to receive the data into.
764 * @bufferlen: Maximum size of what the the buffer will hold
765 * @buffer_actual_len: The actual size of the data after it was received
766 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700767 *
768 * Receives directly from the hyper-v vmbus and puts the data it received
769 * into Buffer. This will receive the data unparsed from hyper-v.
770 *
771 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700772 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700773int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700774 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700775{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700776 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700777 u32 packetlen;
778 u32 userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700779 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800780 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700781
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700782 *buffer_actual_len = 0;
783 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700784
Hank Janssen3e7ee492009-07-13 16:02:34 -0700785
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700786 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700787 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700788 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700789 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700790
Haiyang Zhang415f2282011-01-26 12:12:13 -0800791 packetlen = desc.len8 << 3;
792 userlen = packetlen - (desc.offset8 << 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700793
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700794 *buffer_actual_len = userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700795
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700796 if (userlen > bufferlen) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700797
Hank Janssen0a466182011-03-29 13:58:47 -0700798 pr_err("Buffer too small - got %d needs %d\n",
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700799 bufferlen, userlen);
K. Y. Srinivasan926ae5262011-06-06 15:50:11 -0700800 return -ETOOSMALL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700801 }
802
Haiyang Zhang415f2282011-01-26 12:12:13 -0800803 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700804
Bill Pemberton454f18a2009-07-27 16:47:24 -0400805 /* Copy over the packet to the user buffer */
K. Y. Srinivasan38397c82011-05-10 07:55:25 -0700806 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800807 (desc.offset8 << 3), &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700808
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800809 if (signal)
810 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700811
Hank Janssen3e7ee492009-07-13 16:02:34 -0700812 return 0;
813}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700814EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700815
Hank Janssen3e189512010-03-04 22:11:00 +0000816/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700817 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700818 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700819int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700820 u32 bufferlen, u32 *buffer_actual_len,
821 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700822{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700823 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700824 u32 packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700825 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800826 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700827
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700828 *buffer_actual_len = 0;
829 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700830
Hank Janssen3e7ee492009-07-13 16:02:34 -0700831
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700832 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700833 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700834 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700835 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700836
Hank Janssen3e7ee492009-07-13 16:02:34 -0700837
Haiyang Zhang415f2282011-01-26 12:12:13 -0800838 packetlen = desc.len8 << 3;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700839
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700840 *buffer_actual_len = packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700841
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700842 if (packetlen > bufferlen) {
Hank Janssen0a466182011-03-29 13:58:47 -0700843 pr_err("Buffer too small - needed %d bytes but "
844 "got space for only %d bytes\n",
845 packetlen, bufferlen);
K. Y. Srinivasan3d5cad92011-08-25 09:48:59 -0700846 return -ENOBUFS;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700847 }
848
Haiyang Zhang415f2282011-01-26 12:12:13 -0800849 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700850
Bill Pemberton454f18a2009-07-27 16:47:24 -0400851 /* Copy over the entire packet to the user buffer */
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800852 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
853 &signal);
854
855 if (signal)
856 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700857
Hank Janssen3e7ee492009-07-13 16:02:34 -0700858 return 0;
859}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700860EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);