blob: 740edec161bbfde4d2431d3958a1b89c4109c1a7 [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
Hank Janssen3e7ee492009-07-13 16:02:34 -07002 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssen3e7ee492009-07-13 16:02:34 -070020 */
Hank Janssen0a466182011-03-29 13:58:47 -070021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Greg Kroah-Hartman5654e932009-07-14 15:08:20 -070023#include <linux/kernel.h>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080024#include <linux/sched.h>
25#include <linux/wait.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070026#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Hank Janssenc88c4e42010-05-04 15:55:05 -070028#include <linux/module.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070029#include <linux/hyperv.h>
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -080030#include <linux/uio.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070031
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070032#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070033
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080034#define NUM_PAGES_SPANNED(addr, len) \
35((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
36
Hank Janssen3e189512010-03-04 22:11:00 +000037/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070038 * vmbus_setevent- Trigger an event notification on the specified
Hank Janssen3e189512010-03-04 22:11:00 +000039 * channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070040 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070041static void vmbus_setevent(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -070042{
Haiyang Zhang39d70a42010-09-30 10:52:13 -070043 struct hv_monitor_page *monitorpage;
Hank Janssen3e7ee492009-07-13 16:02:34 -070044
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080045 if (channel->offermsg.monitor_allocated) {
Bill Pemberton454f18a2009-07-27 16:47:24 -040046 /* Each u32 represents 32 channels */
Olaf Hering223565852011-03-21 14:41:37 +010047 sync_set_bit(channel->offermsg.child_relid & 31,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080048 (unsigned long *) vmbus_connection.send_int_page +
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080049 (channel->offermsg.child_relid >> 5));
Hank Janssen3e7ee492009-07-13 16:02:34 -070050
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -070051 /* Get the child to parent monitor page */
52 monitorpage = vmbus_connection.monitor_pages[1];
Hank Janssen3e7ee492009-07-13 16:02:34 -070053
Olaf Hering223565852011-03-21 14:41:37 +010054 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080055 (unsigned long *)&monitorpage->trigger_group
56 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -040057
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070058 } else {
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -080059 vmbus_set_event(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -070060 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070061}
62
Hank Janssen3e189512010-03-04 22:11:00 +000063/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070064 * vmbus_open - Open the specified channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070065 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070066int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -070067 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
68 void (*onchannelcallback)(void *context), void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -070069{
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -070070 struct vmbus_channel_open_channel *open_msg;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -070071 struct vmbus_channel_msginfo *open_info = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -070072 void *in, *out;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070073 unsigned long flags;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -070074 int ret, t, err = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -070075
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070076 spin_lock_irqsave(&newchannel->sc_lock, flags);
77 if (newchannel->state == CHANNEL_OPEN_STATE) {
78 newchannel->state = CHANNEL_OPENING_STATE;
79 } else {
80 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
81 return -EINVAL;
82 }
83 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
84
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080085 newchannel->onchannel_callback = onchannelcallback;
86 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070087
Bill Pemberton454f18a2009-07-27 16:47:24 -040088 /* Allocate the ring buffer */
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -080089 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
90 get_order(send_ringbuffer_size + recv_ringbuffer_size));
91
Bill Pemberton7e052d92010-05-05 15:27:30 -040092 if (!out)
93 return -ENOMEM;
94
Hank Janssen3e7ee492009-07-13 16:02:34 -070095
Haiyang Zhang39d70a42010-09-30 10:52:13 -070096 in = (void *)((unsigned long)out + send_ringbuffer_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -070097
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080098 newchannel->ringbuffer_pages = out;
99 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700100 recv_ringbuffer_size) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700101
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700102 ret = hv_ringbuffer_init(
103 &newchannel->outbound, out, send_ringbuffer_size);
104
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000105 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400106 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700107 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400108 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700109
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700110 ret = hv_ringbuffer_init(
111 &newchannel->inbound, in, recv_ringbuffer_size);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000112 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400113 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700114 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400115 }
116
Hank Janssen3e7ee492009-07-13 16:02:34 -0700117
Bill Pemberton454f18a2009-07-27 16:47:24 -0400118 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800119 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700120
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700121 ret = vmbus_establish_gpadl(newchannel,
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800122 newchannel->outbound.ring_buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700123 send_ringbuffer_size +
124 recv_ringbuffer_size,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800125 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400126
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000127 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400128 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700129 goto error0;
Bill Pembertonb94ef342010-05-05 15:27:37 -0400130 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700131
Bill Pemberton454f18a2009-07-27 16:47:24 -0400132 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700133 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700134 sizeof(struct vmbus_channel_open_channel),
135 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700136 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400137 err = -ENOMEM;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700138 goto error0;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400139 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700140
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700141 init_completion(&open_info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700142
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700143 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700144 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
145 open_msg->openid = newchannel->offermsg.child_relid;
146 open_msg->child_relid = newchannel->offermsg.child_relid;
147 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
148 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700149 PAGE_SHIFT;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800150 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700151
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700152 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400153 err = -EINVAL;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700154 goto error0;
Bill Pembertonc827f942010-05-05 15:27:38 -0400155 }
156
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700157 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700158 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700159
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800160 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700161 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800162 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800163 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700164
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700165 ret = vmbus_post_msg(open_msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700166 sizeof(struct vmbus_channel_open_channel));
Hank Janssen98e08702011-03-29 13:58:44 -0700167
168 if (ret != 0)
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700169 goto error1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700170
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700171 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700172 if (t == 0) {
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800173 err = -ETIMEDOUT;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700174 goto error1;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800175 }
176
Hank Janssen3e7ee492009-07-13 16:02:34 -0700177
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700178 if (open_info->response.open_result.status)
179 err = open_info->response.open_result.status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700180
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800181 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700182 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800183 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700184
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700185 if (err == 0)
186 newchannel->state = CHANNEL_OPENED_STATE;
187
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700188 kfree(open_info);
Hank Janssen98e08702011-03-29 13:58:44 -0700189 return err;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400190
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700191error1:
192 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
193 list_del(&open_info->msglistentry);
194 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
195
196error0:
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800197 free_pages((unsigned long)out,
198 get_order(send_ringbuffer_size + recv_ringbuffer_size));
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700199 kfree(open_info);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400200 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700201}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700202EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700203
Hank Janssen3e189512010-03-04 22:11:00 +0000204/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700205 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700206 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700207static int create_gpadl_header(void *kbuffer, u32 size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700208 struct vmbus_channel_msginfo **msginfo,
209 u32 *messagecount)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700210{
211 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700212 int pagecount;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700213 struct vmbus_channel_gpadl_header *gpadl_header;
214 struct vmbus_channel_gpadl_body *gpadl_body;
215 struct vmbus_channel_msginfo *msgheader;
216 struct vmbus_channel_msginfo *msgbody = NULL;
217 u32 msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700218
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700219 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700220
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700221 pagecount = size >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700222
Bill Pemberton454f18a2009-07-27 16:47:24 -0400223 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700224 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700225 sizeof(struct vmbus_channel_gpadl_header) -
226 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700227 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700228
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700229 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700230 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400231 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700232 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700233 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700234 sizeof(struct gpa_range) + pfncount * sizeof(u64);
235 msgheader = kzalloc(msgsize, GFP_KERNEL);
236 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400237 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700238
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800239 INIT_LIST_HEAD(&msgheader->submsglist);
240 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700241
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700242 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800243 msgheader->msg;
244 gpadl_header->rangecount = 1;
245 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700246 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800247 gpadl_header->range[0].byte_offset = 0;
248 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700249 for (i = 0; i < pfncount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800250 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
251 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700252 *msginfo = msgheader;
253 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700254
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700255 pfnsum = pfncount;
256 pfnleft = pagecount - pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700257
Bill Pemberton454f18a2009-07-27 16:47:24 -0400258 /* how many pfns can we fit */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700259 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700260 sizeof(struct vmbus_channel_gpadl_body);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700261 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700262
Bill Pemberton454f18a2009-07-27 16:47:24 -0400263 /* fill in the body */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700264 while (pfnleft) {
265 if (pfnleft > pfncount)
266 pfncurr = pfncount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700267 else
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700268 pfncurr = pfnleft;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700269
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700270 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700271 sizeof(struct vmbus_channel_gpadl_body) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700272 pfncurr * sizeof(u64);
273 msgbody = kzalloc(msgsize, GFP_KERNEL);
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700274
275 if (!msgbody) {
276 struct vmbus_channel_msginfo *pos = NULL;
277 struct vmbus_channel_msginfo *tmp = NULL;
278 /*
279 * Free up all the allocated messages.
280 */
281 list_for_each_entry_safe(pos, tmp,
282 &msgheader->submsglist,
283 msglistentry) {
284
285 list_del(&pos->msglistentry);
286 kfree(pos);
287 }
288
Bill Pembertond1c250b2010-05-05 15:27:35 -0400289 goto nomem;
K. Y. Srinivasanf38cf9c2011-06-06 15:50:06 -0700290 }
291
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800292 msgbody->msgsize = msgsize;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700293 (*messagecount)++;
294 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800295 (struct vmbus_channel_gpadl_body *)msgbody->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700296
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700297 /*
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700298 * Gpadl is u32 and we are using a pointer which could
299 * be 64-bit
K. Y. Srinivasanf27df642011-06-06 15:49:56 -0700300 * This is governed by the guest/host protocol and
301 * so the hypervisor gurantees that this is ok.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700302 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700303 for (i = 0; i < pfncurr; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800304 gpadl_body->pfn[i] = slow_virt_to_phys(
305 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
306 PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700307
Bill Pemberton454f18a2009-07-27 16:47:24 -0400308 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800309 list_add_tail(&msgbody->msglistentry,
310 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700311 pfnsum += pfncurr;
312 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700313 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700314 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400315 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700316 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700317 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700318 sizeof(struct gpa_range) + pagecount * sizeof(u64);
319 msgheader = kzalloc(msgsize, GFP_KERNEL);
320 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400321 goto nomem;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800322 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700323
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700324 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800325 msgheader->msg;
326 gpadl_header->rangecount = 1;
327 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700328 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800329 gpadl_header->range[0].byte_offset = 0;
330 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700331 for (i = 0; i < pagecount; i++)
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800332 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
333 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700334
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700335 *msginfo = msgheader;
336 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700337 }
338
339 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400340nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700341 kfree(msgheader);
342 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400343 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700344}
345
Hank Janssen3e189512010-03-04 22:11:00 +0000346/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700347 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700348 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700349 * @channel: a channel
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800350 * @kbuffer: from kmalloc or vmalloc
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700351 * @size: page-size multiple
352 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700353 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700354int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700355 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700356{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700357 struct vmbus_channel_gpadl_header *gpadlmsg;
358 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700359 struct vmbus_channel_msginfo *msginfo = NULL;
360 struct vmbus_channel_msginfo *submsginfo;
361 u32 msgcount;
Bill Pemberton53af5452009-09-11 21:46:44 -0400362 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700363 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700364 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400365 int ret = 0;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700366 int t;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700367
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800368 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
369 atomic_inc(&vmbus_connection.next_gpadl_handle);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700370
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700371 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400372 if (ret)
373 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700374
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700375 init_completion(&msginfo->waitevent);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400376
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800377 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
378 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
379 gpadlmsg->child_relid = channel->offermsg.child_relid;
380 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700381
Hank Janssen3e7ee492009-07-13 16:02:34 -0700382
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800383 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800384 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800385 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700386
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800387 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700388
Haiyang Zhangc6977672011-01-26 12:12:08 -0800389 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700390 sizeof(*msginfo));
Hank Janssen98e08702011-03-29 13:58:44 -0700391 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700392 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700393
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700394 if (msgcount > 1) {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800395 list_for_each(curr, &msginfo->submsglist) {
Bill Pemberton53af5452009-09-11 21:46:44 -0400396
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700397 submsginfo = (struct vmbus_channel_msginfo *)curr;
398 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800399 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700400
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800401 gpadl_body->header.msgtype =
402 CHANNELMSG_GPADL_BODY;
403 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700404
Haiyang Zhangc6977672011-01-26 12:12:08 -0800405 ret = vmbus_post_msg(gpadl_body,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800406 submsginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700407 sizeof(*submsginfo));
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000408 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700409 goto cleanup;
Bill Pemberton99259152010-05-05 15:27:36 -0400410
Hank Janssen3e7ee492009-07-13 16:02:34 -0700411 }
412 }
K. Y. Srinivasan40961de2011-06-06 15:50:05 -0700413 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700414 BUG_ON(t == 0);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800415
Hank Janssen3e7ee492009-07-13 16:02:34 -0700416
Bill Pemberton454f18a2009-07-27 16:47:24 -0400417 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800418 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700419
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700420cleanup:
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800421 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800422 list_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800423 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700424
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700425 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700426 return ret;
427}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700428EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700429
Hank Janssen3e189512010-03-04 22:11:00 +0000430/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700431 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700432 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700433int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700434{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700435 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700436 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700437 unsigned long flags;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700438 int ret, t;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700439
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700440 info = kmalloc(sizeof(*info) +
441 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400442 if (!info)
443 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700444
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700445 init_completion(&info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700446
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800447 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700448
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800449 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
450 msg->child_relid = channel->offermsg.child_relid;
451 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700452
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800453 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800454 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800455 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800456 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc6977672011-01-26 12:12:08 -0800457 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700458 sizeof(struct vmbus_channel_gpadl_teardown));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700459
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800460 BUG_ON(ret != 0);
K. Y. Srinivasan40961de2011-06-06 15:50:05 -0700461 t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700462 BUG_ON(t == 0);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700463
Bill Pemberton454f18a2009-07-27 16:47:24 -0400464 /* Received a torndown response */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800465 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800466 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800467 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700468
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700469 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700470 return ret;
471}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700472EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700473
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700474static void reset_channel_cb(void *arg)
475{
476 struct vmbus_channel *channel = arg;
477
478 channel->onchannel_callback = NULL;
479}
480
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700481static void vmbus_close_internal(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700482{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700483 struct vmbus_channel_close_channel *msg;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700484 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700485
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700486 channel->state = CHANNEL_OPEN_STATE;
487 channel->sc_creation_callback = NULL;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400488 /* Stop callback and cancel the timer asap */
K. Y. Srinivasand3ba7202014-04-08 18:45:53 -0700489 if (channel->target_cpu != smp_processor_id())
490 smp_call_function_single(channel->target_cpu, reset_channel_cb,
491 channel, true);
492 else
493 reset_channel_cb(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700494
Bill Pemberton454f18a2009-07-27 16:47:24 -0400495 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700496
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700497 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700498
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800499 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
500 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700501
Haiyang Zhangc6977672011-01-26 12:12:08 -0800502 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700503
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800504 BUG_ON(ret != 0);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400505 /* Tear down the gpadl for the channel's ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800506 if (channel->ringbuffer_gpadlhandle)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700507 vmbus_teardown_gpadl(channel,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800508 channel->ringbuffer_gpadlhandle);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700509
Bill Pemberton454f18a2009-07-27 16:47:24 -0400510 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700511 hv_ringbuffer_cleanup(&channel->outbound);
512 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700513
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800514 free_pages((unsigned long)channel->ringbuffer_pages,
515 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700516
Hank Janssen3e7ee492009-07-13 16:02:34 -0700517
Hank Janssen3e7ee492009-07-13 16:02:34 -0700518}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700519
520/*
521 * vmbus_close - Close the specified channel
522 */
523void vmbus_close(struct vmbus_channel *channel)
524{
525 struct list_head *cur, *tmp;
526 struct vmbus_channel *cur_channel;
527
528 if (channel->primary_channel != NULL) {
529 /*
530 * We will only close sub-channels when
531 * the primary is closed.
532 */
533 return;
534 }
535 /*
536 * Close all the sub-channels first and then close the
537 * primary channel.
538 */
539 list_for_each_safe(cur, tmp, &channel->sc_list) {
540 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
541 if (cur_channel->state != CHANNEL_OPENED_STATE)
542 continue;
543 vmbus_close_internal(cur_channel);
544 }
545 /*
546 * Now close the primary.
547 */
548 vmbus_close_internal(channel);
549}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700550EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700551
Hank Janssenc88c4e42010-05-04 15:55:05 -0700552/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700553 * vmbus_sendpacket() - Send the specified buffer on the given channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700554 * @channel: Pointer to vmbus_channel structure.
555 * @buffer: Pointer to the buffer you want to receive the data into.
556 * @bufferlen: Maximum size of what the the buffer will hold
557 * @requestid: Identifier of the request
558 * @type: Type of packet that is being send e.g. negotiate, time
Hank Janssenc88c4e42010-05-04 15:55:05 -0700559 * packet etc.
560 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700561 * Sends data in @buffer directly to hyper-v via the vmbus
Hank Janssenc88c4e42010-05-04 15:55:05 -0700562 * This will send the data unparsed to hyper-v.
563 *
564 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700565 */
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800566int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700567 u32 bufferlen, u64 requestid,
568 enum vmbus_packet_type type, u32 flags)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700569{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700570 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700571 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100572 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800573 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700574 u64 aligned_data = 0;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700575 int ret;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800576 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700577
Hank Janssen3e7ee492009-07-13 16:02:34 -0700578
Bill Pemberton454f18a2009-07-27 16:47:24 -0400579 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800580 desc.type = type; /* VmbusPacketTypeDataInBand; */
581 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700582 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800583 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
584 desc.len8 = (u16)(packetlen_aligned >> 3);
585 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700586
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800587 bufferlist[0].iov_base = &desc;
588 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
589 bufferlist[1].iov_base = buffer;
590 bufferlist[1].iov_len = bufferlen;
591 bufferlist[2].iov_base = &aligned_data;
592 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700593
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800594 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700595
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800596 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700597 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700598
Hank Janssen3e7ee492009-07-13 16:02:34 -0700599 return ret;
600}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700601EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700602
Hank Janssen3e189512010-03-04 22:11:00 +0000603/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700604 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
Hank Janssen3e189512010-03-04 22:11:00 +0000605 * packets using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700606 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700607int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700608 struct hv_page_buffer pagebuffers[],
609 u32 pagecount, void *buffer, u32 bufferlen,
610 u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700611{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700612 int ret;
613 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000614 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700615 u32 descsize;
616 u32 packetlen;
617 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800618 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700619 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800620 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700621
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700622 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400623 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700624
Hank Janssen3e7ee492009-07-13 16:02:34 -0700625
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700626 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000627 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700628 * largest size we support
629 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700630 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
631 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700632 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700633 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100634 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700635
Bill Pemberton454f18a2009-07-27 16:47:24 -0400636 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800637 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000638 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700639 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
640 desc.length8 = (u16)(packetlen_aligned >> 3);
641 desc.transactionid = requestid;
642 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700643
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700644 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800645 desc.range[i].len = pagebuffers[i].len;
646 desc.range[i].offset = pagebuffers[i].offset;
647 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700648 }
649
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800650 bufferlist[0].iov_base = &desc;
651 bufferlist[0].iov_len = descsize;
652 bufferlist[1].iov_base = buffer;
653 bufferlist[1].iov_len = bufferlen;
654 bufferlist[2].iov_base = &aligned_data;
655 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700656
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800657 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700658
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800659 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700660 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700661
Hank Janssen3e7ee492009-07-13 16:02:34 -0700662 return ret;
663}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700664EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700665
Hank Janssen3e189512010-03-04 22:11:00 +0000666/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700667 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000668 * using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700669 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700670int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700671 struct hv_multipage_buffer *multi_pagebuffer,
672 void *buffer, u32 bufferlen, u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700673{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700674 int ret;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000675 struct vmbus_channel_packet_multipage_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700676 u32 descsize;
677 u32 packetlen;
678 u32 packetlen_aligned;
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800679 struct kvec bufferlist[3];
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700680 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800681 bool signal = false;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800682 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
683 multi_pagebuffer->len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700684
Hank Janssen3e7ee492009-07-13 16:02:34 -0700685
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700686 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
Bill Pemberton002b53e2010-05-05 15:27:39 -0400687 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700688
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700689 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000690 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700691 * the largest size we support
692 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700693 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
694 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700695 sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700696 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100697 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700698
Hank Janssen3e7ee492009-07-13 16:02:34 -0700699
Bill Pemberton454f18a2009-07-27 16:47:24 -0400700 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800701 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000702 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700703 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
704 desc.length8 = (u16)(packetlen_aligned >> 3);
705 desc.transactionid = requestid;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000706 desc.rangecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700707
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800708 desc.range.len = multi_pagebuffer->len;
709 desc.range.offset = multi_pagebuffer->offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700710
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800711 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700712 pfncount * sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700713
K. Y. Srinivasan011a7c32014-02-01 19:02:20 -0800714 bufferlist[0].iov_base = &desc;
715 bufferlist[0].iov_len = descsize;
716 bufferlist[1].iov_base = buffer;
717 bufferlist[1].iov_len = bufferlen;
718 bufferlist[2].iov_base = &aligned_data;
719 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700720
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800721 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700722
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800723 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700724 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700725
Hank Janssen3e7ee492009-07-13 16:02:34 -0700726 return ret;
727}
Greg Kroah-Hartman4cb106f2010-10-21 09:23:59 -0700728EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700729
730/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700731 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700732 * @channel: Pointer to vmbus_channel structure.
733 * @buffer: Pointer to the buffer you want to receive the data into.
734 * @bufferlen: Maximum size of what the the buffer will hold
735 * @buffer_actual_len: The actual size of the data after it was received
736 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700737 *
738 * Receives directly from the hyper-v vmbus and puts the data it received
739 * into Buffer. This will receive the data unparsed from hyper-v.
740 *
741 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700742 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700743int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700744 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700745{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700746 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700747 u32 packetlen;
748 u32 userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700749 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800750 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700751
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700752 *buffer_actual_len = 0;
753 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700754
Hank Janssen3e7ee492009-07-13 16:02:34 -0700755
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700756 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700757 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700758 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700759 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700760
Haiyang Zhang415f2282011-01-26 12:12:13 -0800761 packetlen = desc.len8 << 3;
762 userlen = packetlen - (desc.offset8 << 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700763
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700764 *buffer_actual_len = userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700765
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700766 if (userlen > bufferlen) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700767
Hank Janssen0a466182011-03-29 13:58:47 -0700768 pr_err("Buffer too small - got %d needs %d\n",
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700769 bufferlen, userlen);
K. Y. Srinivasan926ae5262011-06-06 15:50:11 -0700770 return -ETOOSMALL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700771 }
772
Haiyang Zhang415f2282011-01-26 12:12:13 -0800773 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700774
Bill Pemberton454f18a2009-07-27 16:47:24 -0400775 /* Copy over the packet to the user buffer */
K. Y. Srinivasan38397c82011-05-10 07:55:25 -0700776 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800777 (desc.offset8 << 3), &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700778
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800779 if (signal)
780 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700781
Hank Janssen3e7ee492009-07-13 16:02:34 -0700782 return 0;
783}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700784EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700785
Hank Janssen3e189512010-03-04 22:11:00 +0000786/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700787 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700788 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700789int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700790 u32 bufferlen, u32 *buffer_actual_len,
791 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700792{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700793 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700794 u32 packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700795 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800796 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700797
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700798 *buffer_actual_len = 0;
799 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700800
Hank Janssen3e7ee492009-07-13 16:02:34 -0700801
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700802 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700803 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700804 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700805 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700806
Hank Janssen3e7ee492009-07-13 16:02:34 -0700807
Haiyang Zhang415f2282011-01-26 12:12:13 -0800808 packetlen = desc.len8 << 3;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700809
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700810 *buffer_actual_len = packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700811
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700812 if (packetlen > bufferlen) {
Hank Janssen0a466182011-03-29 13:58:47 -0700813 pr_err("Buffer too small - needed %d bytes but "
814 "got space for only %d bytes\n",
815 packetlen, bufferlen);
K. Y. Srinivasan3d5cad92011-08-25 09:48:59 -0700816 return -ENOBUFS;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700817 }
818
Haiyang Zhang415f2282011-01-26 12:12:13 -0800819 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700820
Bill Pemberton454f18a2009-07-27 16:47:24 -0400821 /* Copy over the entire packet to the user buffer */
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800822 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
823 &signal);
824
825 if (signal)
826 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700827
Dan Carpenter33b06932013-09-17 00:58:36 +0300828 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700829}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700830EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);