blob: 406537420fffdc0b90c008dad53205bd19f2acfe [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
Bill Pemberton454f18a2009-07-27 16:47:24 -040036/* Internal routines */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070037static int create_gpadl_header(
Haiyang Zhang39d70a42010-09-30 10:52:13 -070038 void *kbuffer, /* must be phys and virt contiguous */
39 u32 size, /* page-size multiple */
40 struct vmbus_channel_msginfo **msginfo,
41 u32 *messagecount);
Haiyang Zhangfff41b22010-10-07 11:40:08 -070042static void vmbus_setevent(struct vmbus_channel *channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -070043
Hank Janssen3e189512010-03-04 22:11:00 +000044/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070045 * vmbus_setevent- Trigger an event notification on the specified
Hank Janssen3e189512010-03-04 22:11:00 +000046 * channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070047 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070048static void vmbus_setevent(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -070049{
Haiyang Zhang39d70a42010-09-30 10:52:13 -070050 struct hv_monitor_page *monitorpage;
Hank Janssen3e7ee492009-07-13 16:02:34 -070051
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080052 if (channel->offermsg.monitor_allocated) {
Bill Pemberton454f18a2009-07-27 16:47:24 -040053 /* Each u32 represents 32 channels */
Olaf Hering22356582011-03-21 14:41:37 +010054 sync_set_bit(channel->offermsg.child_relid & 31,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080055 (unsigned long *) vmbus_connection.send_int_page +
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080056 (channel->offermsg.child_relid >> 5));
Hank Janssen3e7ee492009-07-13 16:02:34 -070057
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080058 monitorpage = vmbus_connection.monitor_pages;
Haiyang Zhang39d70a42010-09-30 10:52:13 -070059 monitorpage++; /* Get the child to parent monitor page */
Hank Janssen3e7ee492009-07-13 16:02:34 -070060
Olaf Hering22356582011-03-21 14:41:37 +010061 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080062 (unsigned long *)&monitorpage->trigger_group
63 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -040064
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070065 } else {
Haiyang Zhangc6977672011-01-26 12:12:08 -080066 vmbus_set_event(channel->offermsg.child_relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -070067 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070068}
69
Hank Janssen3e189512010-03-04 22:11:00 +000070/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070071 * vmbus_get_debug_info -Retrieve various channel debug info
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070072 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070073void vmbus_get_debug_info(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -070074 struct vmbus_channel_debug_info *debuginfo)
Hank Janssen3e7ee492009-07-13 16:02:34 -070075{
Haiyang Zhang39d70a42010-09-30 10:52:13 -070076 struct hv_monitor_page *monitorpage;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080077 u8 monitor_group = (u8)channel->offermsg.monitorid / 32;
78 u8 monitor_offset = (u8)channel->offermsg.monitorid % 32;
Hank Janssen3e7ee492009-07-13 16:02:34 -070079
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080080 debuginfo->relid = channel->offermsg.child_relid;
81 debuginfo->state = channel->state;
82 memcpy(&debuginfo->interfacetype,
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -070083 &channel->offermsg.offer.if_type, sizeof(uuid_le));
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080084 memcpy(&debuginfo->interface_instance,
Haiyang Zhang767dff62011-01-26 12:12:12 -080085 &channel->offermsg.offer.if_instance,
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -070086 sizeof(uuid_le));
Hank Janssen3e7ee492009-07-13 16:02:34 -070087
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080088 monitorpage = (struct hv_monitor_page *)vmbus_connection.monitor_pages;
Hank Janssen3e7ee492009-07-13 16:02:34 -070089
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080090 debuginfo->monitorid = channel->offermsg.monitorid;
Hank Janssen3e7ee492009-07-13 16:02:34 -070091
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080092 debuginfo->servermonitor_pending =
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080093 monitorpage->trigger_group[monitor_group].pending;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080094 debuginfo->servermonitor_latency =
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080095 monitorpage->latency[monitor_group][monitor_offset];
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080096 debuginfo->servermonitor_connectionid =
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080097 monitorpage->parameter[monitor_group]
98 [monitor_offset].connectionid.u.id;
Hank Janssen3e7ee492009-07-13 16:02:34 -070099
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700100 monitorpage++;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700101
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800102 debuginfo->clientmonitor_pending =
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800103 monitorpage->trigger_group[monitor_group].pending;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800104 debuginfo->clientmonitor_latency =
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800105 monitorpage->latency[monitor_group][monitor_offset];
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800106 debuginfo->clientmonitor_connectionid =
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800107 monitorpage->parameter[monitor_group]
108 [monitor_offset].connectionid.u.id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700109
K. Y. Srinivasana75b61d2011-05-10 07:55:28 -0700110 hv_ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound);
111 hv_ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700112}
113
Hank Janssen3e189512010-03-04 22:11:00 +0000114/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700115 * vmbus_open - Open the specified channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700116 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700117int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700118 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
119 void (*onchannelcallback)(void *context), void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700120{
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700121 struct vmbus_channel_open_channel *open_msg;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700122 struct vmbus_channel_msginfo *open_info = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700123 void *in, *out;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700124 unsigned long flags;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700125 int ret, t, err = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700126
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800127 newchannel->onchannel_callback = onchannelcallback;
128 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700129
Bill Pemberton454f18a2009-07-27 16:47:24 -0400130 /* Allocate the ring buffer */
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800131 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
132 get_order(send_ringbuffer_size + recv_ringbuffer_size));
133
Bill Pemberton7e052d92010-05-05 15:27:30 -0400134 if (!out)
135 return -ENOMEM;
136
Hank Janssen3e7ee492009-07-13 16:02:34 -0700137
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700138 in = (void *)((unsigned long)out + send_ringbuffer_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700139
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800140 newchannel->ringbuffer_pages = out;
141 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700142 recv_ringbuffer_size) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700143
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700144 ret = hv_ringbuffer_init(
145 &newchannel->outbound, out, send_ringbuffer_size);
146
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000147 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400148 err = ret;
149 goto errorout;
150 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700151
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700152 ret = hv_ringbuffer_init(
153 &newchannel->inbound, in, recv_ringbuffer_size);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000154 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400155 err = ret;
156 goto errorout;
157 }
158
Hank Janssen3e7ee492009-07-13 16:02:34 -0700159
Bill Pemberton454f18a2009-07-27 16:47:24 -0400160 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800161 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700162
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700163 ret = vmbus_establish_gpadl(newchannel,
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800164 newchannel->outbound.ring_buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700165 send_ringbuffer_size +
166 recv_ringbuffer_size,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800167 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400168
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000169 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400170 err = ret;
171 goto errorout;
172 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700173
Bill Pemberton454f18a2009-07-27 16:47:24 -0400174 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700175 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700176 sizeof(struct vmbus_channel_open_channel),
177 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700178 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400179 err = -ENOMEM;
180 goto errorout;
181 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700182
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700183 init_completion(&open_info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700184
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700185 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700186 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
187 open_msg->openid = newchannel->offermsg.child_relid;
188 open_msg->child_relid = newchannel->offermsg.child_relid;
189 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
190 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700191 PAGE_SHIFT;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700192 open_msg->server_contextarea_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700193
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700194 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400195 err = -EINVAL;
196 goto errorout;
197 }
198
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700199 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700200 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700201
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800202 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700203 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800204 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800205 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700206
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700207 ret = vmbus_post_msg(open_msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700208 sizeof(struct vmbus_channel_open_channel));
Hank Janssen98e08702011-03-29 13:58:44 -0700209
210 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700211 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700212
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700213 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700214 if (t == 0) {
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800215 err = -ETIMEDOUT;
216 goto errorout;
217 }
218
Hank Janssen3e7ee492009-07-13 16:02:34 -0700219
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700220 if (open_info->response.open_result.status)
221 err = open_info->response.open_result.status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700222
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700223cleanup:
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. Srinivasan176fb9e2011-08-25 09:48:57 -0700228 kfree(open_info);
Hank Janssen98e08702011-03-29 13:58:44 -0700229 return err;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400230
231errorout:
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700232 hv_ringbuffer_cleanup(&newchannel->outbound);
233 hv_ringbuffer_cleanup(&newchannel->inbound);
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800234 free_pages((unsigned long)out,
235 get_order(send_ringbuffer_size + recv_ringbuffer_size));
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700236 kfree(open_info);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400237 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700238}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700239EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700240
Hank Janssen3e189512010-03-04 22:11:00 +0000241/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700242 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700243 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700244static int create_gpadl_header(void *kbuffer, u32 size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700245 struct vmbus_channel_msginfo **msginfo,
246 u32 *messagecount)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700247{
248 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700249 int pagecount;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700250 unsigned long long pfn;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700251 struct vmbus_channel_gpadl_header *gpadl_header;
252 struct vmbus_channel_gpadl_body *gpadl_body;
253 struct vmbus_channel_msginfo *msgheader;
254 struct vmbus_channel_msginfo *msgbody = NULL;
255 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700256
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700257 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700258
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700259 pagecount = size >> PAGE_SHIFT;
260 pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700261
Bill Pemberton454f18a2009-07-27 16:47:24 -0400262 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700263 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700264 sizeof(struct vmbus_channel_gpadl_header) -
265 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700266 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700267
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700268 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700269 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400270 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700271 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700272 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700273 sizeof(struct gpa_range) + pfncount * sizeof(u64);
274 msgheader = kzalloc(msgsize, GFP_KERNEL);
275 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400276 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700277
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800278 INIT_LIST_HEAD(&msgheader->submsglist);
279 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700280
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700281 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800282 msgheader->msg;
283 gpadl_header->rangecount = 1;
284 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700285 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800286 gpadl_header->range[0].byte_offset = 0;
287 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700288 for (i = 0; i < pfncount; i++)
Haiyang Zhang415f2282011-01-26 12:12:13 -0800289 gpadl_header->range[0].pfn_array[i] = pfn+i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700290 *msginfo = msgheader;
291 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700292
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700293 pfnsum = pfncount;
294 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700295
Bill Pemberton454f18a2009-07-27 16:47:24 -0400296 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700297 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700298 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700299 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700300
Bill Pemberton454f18a2009-07-27 16:47:24 -0400301 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700302 while (pfnleft) {
303 if (pfnleft > pfncount)
304 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700305 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700306 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700307
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700308 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700309 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700310 pfncurr * sizeof(u64);
311 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700312
313 if (!msgbody) {
314 struct vmbus_channel_msginfo *pos = NULL;
315 struct vmbus_channel_msginfo *tmp = NULL;
316 /*
317 * Free up all the allocated messages.
318 */
319 list_for_each_entry_safe(pos, tmp,
320 &msgheader->submsglist,
321 msglistentry) {
322
323 list_del(&pos->msglistentry);
324 kfree(pos);
325 }
326
Bill Pembertond1c250b2010-05-05 15:27:35 -0400327 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700328 }
329
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800330 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700331 (*messagecount)++;
332 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800333 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700334
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700335 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700336 * Gpadl is u32 and we are using a pointer which could
337 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700338 * This is governed by the guest/host protocol and
339 * so the hypervisor gurantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700340 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700341 for (i = 0; i < pfncurr; i++)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800342 gpadl_body->pfn[i] = pfn + pfnsum + i;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700343
Bill Pemberton454f18a2009-07-27 16:47:24 -0400344 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800345 list_add_tail(&msgbody->msglistentry,
346 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700347 pfnsum += pfncurr;
348 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700349 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700350 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400351 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700352 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700353 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700354 sizeof(struct gpa_range) + pagecount * sizeof(u64);
355 msgheader = kzalloc(msgsize, GFP_KERNEL);
356 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400357 goto nomem;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800358 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700359
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700360 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800361 msgheader->msg;
362 gpadl_header->rangecount = 1;
363 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700364 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800365 gpadl_header->range[0].byte_offset = 0;
366 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700367 for (i = 0; i < pagecount; i++)
Haiyang Zhang415f2282011-01-26 12:12:13 -0800368 gpadl_header->range[0].pfn_array[i] = pfn+i;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700369
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700370 *msginfo = msgheader;
371 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700372 }
373
374 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400375nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700376 kfree(msgheader);
377 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400378 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700379}
380
Hank Janssen3e189512010-03-04 22:11:00 +0000381/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700382 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700383 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700384 * @channel: a channel
385 * @kbuffer: from kmalloc
386 * @size: page-size multiple
387 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700388 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700389int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700390 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700391{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700392 struct vmbus_channel_gpadl_header *gpadlmsg;
393 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700394 struct vmbus_channel_msginfo *msginfo = NULL;
395 struct vmbus_channel_msginfo *submsginfo;
396 u32 msgcount;
Bill Pemberton53af5452009-09-11 21:46:44 -0400397 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700398 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700399 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400400 int ret = 0;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700401 int t;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700402
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800403 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
404 atomic_inc(&vmbus_connection.next_gpadl_handle);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700405
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700406 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400407 if (ret)
408 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700409
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700410 init_completion(&msginfo->waitevent);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400411
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800412 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
413 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
414 gpadlmsg->child_relid = channel->offermsg.child_relid;
415 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700416
Hank Janssen3e7ee492009-07-13 16:02:34 -0700417
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800418 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800419 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800420 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700421
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800422 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700423
Haiyang Zhangc6977672011-01-26 12:12:08 -0800424 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700425 sizeof(*msginfo));
Hank Janssen98e08702011-03-29 13:58:44 -0700426 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700427 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700428
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700429 if (msgcount > 1) {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800430 list_for_each(curr, &msginfo->submsglist) {
Bill Pemberton53af5452009-09-11 21:46:44 -0400431
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700432 submsginfo = (struct vmbus_channel_msginfo *)curr;
433 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800434 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700435
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800436 gpadl_body->header.msgtype =
437 CHANNELMSG_GPADL_BODY;
438 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700439
Haiyang Zhangc6977672011-01-26 12:12:08 -0800440 ret = vmbus_post_msg(gpadl_body,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800441 submsginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700442 sizeof(*submsginfo));
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000443 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700444 goto cleanup;
Bill Pemberton99259152010-05-05 15:27:36 -0400445
Hank Janssen3e7ee492009-07-13 16:02:34 -0700446 }
447 }
K. Y. Srinivasan40961de2011-06-06 15:50:05 -0700448 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700449 BUG_ON(t == 0);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800450
Hank Janssen3e7ee492009-07-13 16:02:34 -0700451
Bill Pemberton454f18a2009-07-27 16:47:24 -0400452 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800453 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700454
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700455cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800456 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800457 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800458 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700459
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700460 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700461 return ret;
462}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700463EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700464
Hank Janssen3e189512010-03-04 22:11:00 +0000465/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700466 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700467 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700468int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700469{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700470 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700471 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700472 unsigned long flags;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700473 int ret, t;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700474
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700475 info = kmalloc(sizeof(*info) +
476 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400477 if (!info)
478 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700479
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700480 init_completion(&info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700481
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800482 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700483
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800484 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
485 msg->child_relid = channel->offermsg.child_relid;
486 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700487
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800488 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800489 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800490 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800491 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc6977672011-01-26 12:12:08 -0800492 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700493 sizeof(struct vmbus_channel_gpadl_teardown));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700494
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800495 BUG_ON(ret != 0);
K. Y. Srinivasan40961de2011-06-06 15:50:05 -0700496 t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700497 BUG_ON(t == 0);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700498
Bill Pemberton454f18a2009-07-27 16:47:24 -0400499 /* Received a torndown response */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800500 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800501 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800502 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700503
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700504 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700505 return ret;
506}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700507EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700508
Hank Janssen3e189512010-03-04 22:11:00 +0000509/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700510 * vmbus_close - Close the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700511 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700512void vmbus_close(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700513{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700514 struct vmbus_channel_close_channel *msg;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700515 int ret;
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700516 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700517
Bill Pemberton454f18a2009-07-27 16:47:24 -0400518 /* Stop callback and cancel the timer asap */
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700519 spin_lock_irqsave(&channel->inbound_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800520 channel->onchannel_callback = NULL;
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700521 spin_unlock_irqrestore(&channel->inbound_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700522
Bill Pemberton454f18a2009-07-27 16:47:24 -0400523 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700524
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700525 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700526
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800527 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
528 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700529
Haiyang Zhangc6977672011-01-26 12:12:08 -0800530 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700531
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800532 BUG_ON(ret != 0);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400533 /* Tear down the gpadl for the channel's ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800534 if (channel->ringbuffer_gpadlhandle)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700535 vmbus_teardown_gpadl(channel,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800536 channel->ringbuffer_gpadlhandle);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700537
Bill Pemberton454f18a2009-07-27 16:47:24 -0400538 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700539 hv_ringbuffer_cleanup(&channel->outbound);
540 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700541
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800542 free_pages((unsigned long)channel->ringbuffer_pages,
543 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700544
Hank Janssen3e7ee492009-07-13 16:02:34 -0700545
Hank Janssen3e7ee492009-07-13 16:02:34 -0700546}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700547EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700548
Hank Janssenc88c4e42010-05-04 15:55:05 -0700549/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700550 * vmbus_sendpacket() - Send the specified buffer on the given channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700551 * @channel: Pointer to vmbus_channel structure.
552 * @buffer: Pointer to the buffer you want to receive the data into.
553 * @bufferlen: Maximum size of what the the buffer will hold
554 * @requestid: Identifier of the request
555 * @type: Type of packet that is being send e.g. negotiate, time
Hank Janssenc88c4e42010-05-04 15:55:05 -0700556 * packet etc.
557 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700558 * Sends data in @buffer directly to hyper-v via the vmbus
Hank Janssenc88c4e42010-05-04 15:55:05 -0700559 * This will send the data unparsed to hyper-v.
560 *
561 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700562 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700563int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700564 u32 bufferlen, u64 requestid,
565 enum vmbus_packet_type type, u32 flags)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700566{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700567 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700568 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100569 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700570 struct scatterlist bufferlist[3];
571 u64 aligned_data = 0;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700572 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700573
Hank Janssen3e7ee492009-07-13 16:02:34 -0700574
Bill Pemberton454f18a2009-07-27 16:47:24 -0400575 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800576 desc.type = type; /* VmbusPacketTypeDataInBand; */
577 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700578 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800579 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
580 desc.len8 = (u16)(packetlen_aligned >> 3);
581 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700582
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700583 sg_init_table(bufferlist, 3);
584 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
585 sg_set_buf(&bufferlist[1], buffer, bufferlen);
586 sg_set_buf(&bufferlist[2], &aligned_data,
587 packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700588
K. Y. Srinivasan633c4dc2011-05-10 07:55:23 -0700589 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700590
K. Y. Srinivasandecc49d2011-05-10 07:55:26 -0700591 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700592 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700593
Hank Janssen3e7ee492009-07-13 16:02:34 -0700594 return ret;
595}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700596EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700597
Hank Janssen3e189512010-03-04 22:11:00 +0000598/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700599 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
Hank Janssen3e189512010-03-04 22:11:00 +0000600 * packets using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700601 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700602int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700603 struct hv_page_buffer pagebuffers[],
604 u32 pagecount, void *buffer, u32 bufferlen,
605 u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700606{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700607 int ret;
608 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000609 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700610 u32 descsize;
611 u32 packetlen;
612 u32 packetlen_aligned;
613 struct scatterlist bufferlist[3];
614 u64 aligned_data = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700615
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700616 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400617 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700618
Hank Janssen3e7ee492009-07-13 16:02:34 -0700619
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700620 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000621 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700622 * largest size we support
623 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700624 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
625 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700626 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700627 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100628 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
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 = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000632 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700633 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
634 desc.length8 = (u16)(packetlen_aligned >> 3);
635 desc.transactionid = requestid;
636 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700637
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700638 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800639 desc.range[i].len = pagebuffers[i].len;
640 desc.range[i].offset = pagebuffers[i].offset;
641 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700642 }
643
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700644 sg_init_table(bufferlist, 3);
645 sg_set_buf(&bufferlist[0], &desc, descsize);
646 sg_set_buf(&bufferlist[1], buffer, bufferlen);
647 sg_set_buf(&bufferlist[2], &aligned_data,
648 packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700649
K. Y. Srinivasan633c4dc2011-05-10 07:55:23 -0700650 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700651
K. Y. Srinivasandecc49d2011-05-10 07:55:26 -0700652 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700653 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700654
Hank Janssen3e7ee492009-07-13 16:02:34 -0700655 return ret;
656}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700657EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700658
Hank Janssen3e189512010-03-04 22:11:00 +0000659/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700660 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000661 * using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700662 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700663int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700664 struct hv_multipage_buffer *multi_pagebuffer,
665 void *buffer, u32 bufferlen, u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700666{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700667 int ret;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000668 struct vmbus_channel_packet_multipage_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700669 u32 descsize;
670 u32 packetlen;
671 u32 packetlen_aligned;
672 struct scatterlist bufferlist[3];
673 u64 aligned_data = 0;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800674 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
675 multi_pagebuffer->len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700676
Hank Janssen3e7ee492009-07-13 16:02:34 -0700677
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700678 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
Bill Pemberton002b53e2010-05-05 15:27:39 -0400679 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700680
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700681 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000682 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700683 * the largest size we support
684 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700685 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
686 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700687 sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700688 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100689 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700690
Hank Janssen3e7ee492009-07-13 16:02:34 -0700691
Bill Pemberton454f18a2009-07-27 16:47:24 -0400692 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800693 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000694 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700695 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
696 desc.length8 = (u16)(packetlen_aligned >> 3);
697 desc.transactionid = requestid;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000698 desc.rangecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700699
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800700 desc.range.len = multi_pagebuffer->len;
701 desc.range.offset = multi_pagebuffer->offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700702
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800703 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700704 pfncount * sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700705
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700706 sg_init_table(bufferlist, 3);
707 sg_set_buf(&bufferlist[0], &desc, descsize);
708 sg_set_buf(&bufferlist[1], buffer, bufferlen);
709 sg_set_buf(&bufferlist[2], &aligned_data,
710 packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700711
K. Y. Srinivasan633c4dc2011-05-10 07:55:23 -0700712 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700713
K. Y. Srinivasandecc49d2011-05-10 07:55:26 -0700714 if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700715 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700716
Hank Janssen3e7ee492009-07-13 16:02:34 -0700717 return ret;
718}
Greg Kroah-Hartman4cb106f2010-10-21 09:23:59 -0700719EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700720
721/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700722 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700723 * @channel: Pointer to vmbus_channel structure.
724 * @buffer: Pointer to the buffer you want to receive the data into.
725 * @bufferlen: Maximum size of what the the buffer will hold
726 * @buffer_actual_len: The actual size of the data after it was received
727 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700728 *
729 * Receives directly from the hyper-v vmbus and puts the data it received
730 * into Buffer. This will receive the data unparsed from hyper-v.
731 *
732 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700733 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700734int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700735 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700736{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700737 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700738 u32 packetlen;
739 u32 userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700740 int ret;
741
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700742 *buffer_actual_len = 0;
743 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700744
Hank Janssen3e7ee492009-07-13 16:02:34 -0700745
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700746 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700747 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700748 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700749 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700750
Haiyang Zhang415f2282011-01-26 12:12:13 -0800751 packetlen = desc.len8 << 3;
752 userlen = packetlen - (desc.offset8 << 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700753
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700754 *buffer_actual_len = userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700755
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700756 if (userlen > bufferlen) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700757
Hank Janssen0a466182011-03-29 13:58:47 -0700758 pr_err("Buffer too small - got %d needs %d\n",
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700759 bufferlen, userlen);
K. Y. Srinivasan926ae5262011-06-06 15:50:11 -0700760 return -ETOOSMALL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700761 }
762
Haiyang Zhang415f2282011-01-26 12:12:13 -0800763 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700764
Bill Pemberton454f18a2009-07-27 16:47:24 -0400765 /* Copy over the packet to the user buffer */
K. Y. Srinivasan38397c82011-05-10 07:55:25 -0700766 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800767 (desc.offset8 << 3));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700768
Hank Janssen3e7ee492009-07-13 16:02:34 -0700769
Hank Janssen3e7ee492009-07-13 16:02:34 -0700770 return 0;
771}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700772EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700773
Hank Janssen3e189512010-03-04 22:11:00 +0000774/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700775 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700776 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700777int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700778 u32 bufferlen, u32 *buffer_actual_len,
779 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700780{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700781 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700782 u32 packetlen;
783 u32 userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700784 int ret;
785
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700786 *buffer_actual_len = 0;
787 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700788
Hank Janssen3e7ee492009-07-13 16:02:34 -0700789
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700790 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700791 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700792 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700793 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700794
Hank Janssen3e7ee492009-07-13 16:02:34 -0700795
Haiyang Zhang415f2282011-01-26 12:12:13 -0800796 packetlen = desc.len8 << 3;
797 userlen = packetlen - (desc.offset8 << 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700798
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700799 *buffer_actual_len = packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700800
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700801 if (packetlen > bufferlen) {
Hank Janssen0a466182011-03-29 13:58:47 -0700802 pr_err("Buffer too small - needed %d bytes but "
803 "got space for only %d bytes\n",
804 packetlen, bufferlen);
K. Y. Srinivasan3d5cad92011-08-25 09:48:59 -0700805 return -ENOBUFS;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700806 }
807
Haiyang Zhang415f2282011-01-26 12:12:13 -0800808 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700809
Bill Pemberton454f18a2009-07-27 16:47:24 -0400810 /* Copy over the entire packet to the user buffer */
K. Y. Srinivasan38397c82011-05-10 07:55:25 -0700811 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700812
Hank Janssen3e7ee492009-07-13 16:02:34 -0700813 return 0;
814}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700815EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);