blob: cea623c36ae23cc007138371b14ef05e770fe6d0 [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
Hank Janssen3e7ee492009-07-13 16:02:34 -07002 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssen3e7ee492009-07-13 16:02:34 -070020 */
Hank Janssen0a466182011-03-29 13:58:47 -070021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Greg Kroah-Hartman5654e932009-07-14 15:08:20 -070023#include <linux/kernel.h>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080024#include <linux/sched.h>
25#include <linux/wait.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070026#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Hank Janssenc88c4e42010-05-04 15:55:05 -070028#include <linux/module.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070029#include <linux/hyperv.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070030
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070031#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070032
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080033#define NUM_PAGES_SPANNED(addr, len) \
34((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
35
Hank Janssen3e189512010-03-04 22:11:00 +000036/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070037 * vmbus_setevent- Trigger an event notification on the specified
Hank Janssen3e189512010-03-04 22:11:00 +000038 * channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070039 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070040static void vmbus_setevent(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -070041{
Haiyang Zhang39d70a42010-09-30 10:52:13 -070042 struct hv_monitor_page *monitorpage;
Hank Janssen3e7ee492009-07-13 16:02:34 -070043
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080044 if (channel->offermsg.monitor_allocated) {
Bill Pemberton454f18a2009-07-27 16:47:24 -040045 /* Each u32 represents 32 channels */
Olaf Hering22356582011-03-21 14:41:37 +010046 sync_set_bit(channel->offermsg.child_relid & 31,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080047 (unsigned long *) vmbus_connection.send_int_page +
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080048 (channel->offermsg.child_relid >> 5));
Hank Janssen3e7ee492009-07-13 16:02:34 -070049
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -070050 /* Get the child to parent monitor page */
51 monitorpage = vmbus_connection.monitor_pages[1];
Hank Janssen3e7ee492009-07-13 16:02:34 -070052
Olaf Hering22356582011-03-21 14:41:37 +010053 sync_set_bit(channel->monitor_bit,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080054 (unsigned long *)&monitorpage->trigger_group
55 [channel->monitor_grp].pending);
Bill Pemberton7c369f42009-07-29 17:00:11 -040056
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070057 } else {
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -080058 vmbus_set_event(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -070059 }
Hank Janssen3e7ee492009-07-13 16:02:34 -070060}
61
Hank Janssen3e189512010-03-04 22:11:00 +000062/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -070063 * vmbus_open - Open the specified channel.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -070064 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -070065int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -070066 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
67 void (*onchannelcallback)(void *context), void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -070068{
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -070069 struct vmbus_channel_open_channel *open_msg;
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -070070 struct vmbus_channel_msginfo *open_info = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -070071 void *in, *out;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -070072 unsigned long flags;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -070073 int ret, t, err = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -070074
K. Y. Srinivasane68d2972013-05-23 12:02:32 -070075 spin_lock_irqsave(&newchannel->sc_lock, flags);
76 if (newchannel->state == CHANNEL_OPEN_STATE) {
77 newchannel->state = CHANNEL_OPENING_STATE;
78 } else {
79 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
80 return -EINVAL;
81 }
82 spin_unlock_irqrestore(&newchannel->sc_lock, flags);
83
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080084 newchannel->onchannel_callback = onchannelcallback;
85 newchannel->channel_callback_context = context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070086
Bill Pemberton454f18a2009-07-27 16:47:24 -040087 /* Allocate the ring buffer */
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -080088 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
89 get_order(send_ringbuffer_size + recv_ringbuffer_size));
90
Bill Pemberton7e052d92010-05-05 15:27:30 -040091 if (!out)
92 return -ENOMEM;
93
Hank Janssen3e7ee492009-07-13 16:02:34 -070094
Haiyang Zhang39d70a42010-09-30 10:52:13 -070095 in = (void *)((unsigned long)out + send_ringbuffer_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -070096
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080097 newchannel->ringbuffer_pages = out;
98 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
Haiyang Zhang39d70a42010-09-30 10:52:13 -070099 recv_ringbuffer_size) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700100
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700101 ret = hv_ringbuffer_init(
102 &newchannel->outbound, out, send_ringbuffer_size);
103
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000104 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400105 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700106 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400107 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700108
K. Y. Srinivasan72a95cb2011-05-10 07:55:21 -0700109 ret = hv_ringbuffer_init(
110 &newchannel->inbound, in, recv_ringbuffer_size);
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000111 if (ret != 0) {
Bill Pemberton3324fb42010-05-05 15:27:49 -0400112 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700113 goto error0;
Bill Pemberton3324fb42010-05-05 15:27:49 -0400114 }
115
Hank Janssen3e7ee492009-07-13 16:02:34 -0700116
Bill Pemberton454f18a2009-07-27 16:47:24 -0400117 /* Establish the gpadl for the ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800118 newchannel->ringbuffer_gpadlhandle = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700119
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700120 ret = vmbus_establish_gpadl(newchannel,
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800121 newchannel->outbound.ring_buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700122 send_ringbuffer_size +
123 recv_ringbuffer_size,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800124 &newchannel->ringbuffer_gpadlhandle);
Bill Pembertonb94ef342010-05-05 15:27:37 -0400125
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000126 if (ret != 0) {
Bill Pembertonb94ef342010-05-05 15:27:37 -0400127 err = ret;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700128 goto error0;
Bill Pembertonb94ef342010-05-05 15:27:37 -0400129 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700130
Bill Pemberton454f18a2009-07-27 16:47:24 -0400131 /* Create and init the channel open message */
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700132 open_info = kmalloc(sizeof(*open_info) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700133 sizeof(struct vmbus_channel_open_channel),
134 GFP_KERNEL);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700135 if (!open_info) {
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400136 err = -ENOMEM;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700137 goto error0;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400138 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700139
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700140 init_completion(&open_info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700141
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700142 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700143 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
144 open_msg->openid = newchannel->offermsg.child_relid;
145 open_msg->child_relid = newchannel->offermsg.child_relid;
146 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
147 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700148 PAGE_SHIFT;
K. Y. Srinivasanabbf3b22012-12-01 06:46:48 -0800149 open_msg->target_vp = newchannel->target_vp;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700150
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700151 if (userdatalen > MAX_USER_DEFINED_BYTES) {
Bill Pembertonc827f942010-05-05 15:27:38 -0400152 err = -EINVAL;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700153 goto error0;
Bill Pembertonc827f942010-05-05 15:27:38 -0400154 }
155
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700156 if (userdatalen)
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700157 memcpy(open_msg->userdata, userdata, userdatalen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700158
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800159 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700160 list_add_tail(&open_info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800161 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800162 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700163
K. Y. Srinivasan0987ff62011-08-25 09:48:55 -0700164 ret = vmbus_post_msg(open_msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700165 sizeof(struct vmbus_channel_open_channel));
Hank Janssen98e08702011-03-29 13:58:44 -0700166
167 if (ret != 0)
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700168 goto error1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700169
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700170 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700171 if (t == 0) {
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800172 err = -ETIMEDOUT;
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700173 goto error1;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800174 }
175
Hank Janssen3e7ee492009-07-13 16:02:34 -0700176
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700177 if (open_info->response.open_result.status)
178 err = open_info->response.open_result.status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700179
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800180 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700181 list_del(&open_info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800182 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700183
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700184 if (err == 0)
185 newchannel->state = CHANNEL_OPENED_STATE;
186
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700187 kfree(open_info);
Hank Janssen98e08702011-03-29 13:58:44 -0700188 return err;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400189
K. Y. Srinivasan13925502012-10-12 13:22:42 -0700190error1:
191 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
192 list_del(&open_info->msglistentry);
193 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
194
195error0:
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800196 free_pages((unsigned long)out,
197 get_order(send_ringbuffer_size + recv_ringbuffer_size));
K. Y. Srinivasan176fb9e2011-08-25 09:48:57 -0700198 kfree(open_info);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400199 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700200}
Greg Kroah-Hartman36ceadf2010-10-21 09:58:21 -0700201EXPORT_SYMBOL_GPL(vmbus_open);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700202
Hank Janssen3e189512010-03-04 22:11:00 +0000203/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700204 * create_gpadl_header - Creates a gpadl for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700205 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700206static int create_gpadl_header(void *kbuffer, u32 size,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700207 struct vmbus_channel_msginfo **msginfo,
208 u32 *messagecount)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700209{
210 int i;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700211 int pagecount;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700212 unsigned long long pfn;
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;
222 pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700223
Bill Pemberton454f18a2009-07-27 16:47:24 -0400224 /* do we need a gpadl body msg */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700225 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700226 sizeof(struct vmbus_channel_gpadl_header) -
227 sizeof(struct gpa_range);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700228 pfncount = pfnsize / sizeof(u64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700229
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700230 if (pagecount > pfncount) {
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700231 /* we need a gpadl body */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400232 /* fill in the header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700233 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700234 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700235 sizeof(struct gpa_range) + pfncount * sizeof(u64);
236 msgheader = kzalloc(msgsize, GFP_KERNEL);
237 if (!msgheader)
Bill Pembertond1c250b2010-05-05 15:27:35 -0400238 goto nomem;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700239
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800240 INIT_LIST_HEAD(&msgheader->submsglist);
241 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700242
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700243 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800244 msgheader->msg;
245 gpadl_header->rangecount = 1;
246 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700247 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800248 gpadl_header->range[0].byte_offset = 0;
249 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700250 for (i = 0; i < pfncount; i++)
Haiyang Zhang415f2282011-01-26 12:12:13 -0800251 gpadl_header->range[0].pfn_array[i] = pfn+i;
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 Zhangc50f7fb2010-11-08 14:04:38 -0800304 gpadl_body->pfn[i] = pfn + pfnsum + i;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700305
Bill Pemberton454f18a2009-07-27 16:47:24 -0400306 /* add to msg header */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800307 list_add_tail(&msgbody->msglistentry,
308 &msgheader->submsglist);
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700309 pfnsum += pfncurr;
310 pfnleft -= pfncurr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700311 }
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700312 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400313 /* everything fits in a header */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700314 msgsize = sizeof(struct vmbus_channel_msginfo) +
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700315 sizeof(struct vmbus_channel_gpadl_header) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700316 sizeof(struct gpa_range) + pagecount * sizeof(u64);
317 msgheader = kzalloc(msgsize, GFP_KERNEL);
318 if (msgheader == NULL)
Kulikov Vasiliye3eb7cd2010-07-16 20:13:51 +0400319 goto nomem;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800320 msgheader->msgsize = msgsize;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700321
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700322 gpadl_header = (struct vmbus_channel_gpadl_header *)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800323 msgheader->msg;
324 gpadl_header->rangecount = 1;
325 gpadl_header->range_buflen = sizeof(struct gpa_range) +
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700326 pagecount * sizeof(u64);
Haiyang Zhang415f2282011-01-26 12:12:13 -0800327 gpadl_header->range[0].byte_offset = 0;
328 gpadl_header->range[0].byte_count = size;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700329 for (i = 0; i < pagecount; i++)
Haiyang Zhang415f2282011-01-26 12:12:13 -0800330 gpadl_header->range[0].pfn_array[i] = pfn+i;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700331
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700332 *msginfo = msgheader;
333 *messagecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700334 }
335
336 return 0;
Bill Pembertond1c250b2010-05-05 15:27:35 -0400337nomem:
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700338 kfree(msgheader);
339 kfree(msgbody);
Bill Pembertond1c250b2010-05-05 15:27:35 -0400340 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700341}
342
Hank Janssen3e189512010-03-04 22:11:00 +0000343/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700344 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700345 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700346 * @channel: a channel
347 * @kbuffer: from kmalloc
348 * @size: page-size multiple
349 * @gpadl_handle: some funky thing
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700350 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700351int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700352 u32 size, u32 *gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700353{
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700354 struct vmbus_channel_gpadl_header *gpadlmsg;
355 struct vmbus_channel_gpadl_body *gpadl_body;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700356 struct vmbus_channel_msginfo *msginfo = NULL;
357 struct vmbus_channel_msginfo *submsginfo;
358 u32 msgcount;
Bill Pemberton53af5452009-09-11 21:46:44 -0400359 struct list_head *curr;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700360 u32 next_gpadl_handle;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700361 unsigned long flags;
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400362 int ret = 0;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700363 int t;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700364
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800365 next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
366 atomic_inc(&vmbus_connection.next_gpadl_handle);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700367
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700368 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400369 if (ret)
370 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700371
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700372 init_completion(&msginfo->waitevent);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400373
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800374 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
375 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
376 gpadlmsg->child_relid = channel->offermsg.child_relid;
377 gpadlmsg->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700378
Hank Janssen3e7ee492009-07-13 16:02:34 -0700379
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800380 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800381 list_add_tail(&msginfo->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800382 &vmbus_connection.chn_msg_list);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700383
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800384 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700385
Haiyang Zhangc6977672011-01-26 12:12:08 -0800386 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700387 sizeof(*msginfo));
Hank Janssen98e08702011-03-29 13:58:44 -0700388 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700389 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700390
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700391 if (msgcount > 1) {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800392 list_for_each(curr, &msginfo->submsglist) {
Bill Pemberton53af5452009-09-11 21:46:44 -0400393
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700394 submsginfo = (struct vmbus_channel_msginfo *)curr;
395 gpadl_body =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800396 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700397
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800398 gpadl_body->header.msgtype =
399 CHANNELMSG_GPADL_BODY;
400 gpadl_body->gpadl = next_gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700401
Haiyang Zhangc6977672011-01-26 12:12:08 -0800402 ret = vmbus_post_msg(gpadl_body,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800403 submsginfo->msgsize -
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700404 sizeof(*submsginfo));
Haiyang Zhangfd4dc882010-05-13 15:56:30 +0000405 if (ret != 0)
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700406 goto cleanup;
Bill Pemberton99259152010-05-05 15:27:36 -0400407
Hank Janssen3e7ee492009-07-13 16:02:34 -0700408 }
409 }
K. Y. Srinivasan40961de2011-06-06 15:50:05 -0700410 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700411 BUG_ON(t == 0);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800412
Hank Janssen3e7ee492009-07-13 16:02:34 -0700413
Bill Pemberton454f18a2009-07-27 16:47:24 -0400414 /* At this point, we received the gpadl created msg */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800415 *gpadl_handle = gpadlmsg->gpadl;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700416
K. Y. Srinivasan00d760b2011-06-06 15:50:12 -0700417cleanup:
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_del(&msginfo->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800420 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700421
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700422 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700423 return ret;
424}
Greg Kroah-Hartman98873722010-10-21 08:47:43 -0700425EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700426
Hank Janssen3e189512010-03-04 22:11:00 +0000427/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700428 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700429 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700430int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700431{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700432 struct vmbus_channel_gpadl_teardown *msg;
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700433 struct vmbus_channel_msginfo *info;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700434 unsigned long flags;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700435 int ret, t;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700436
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700437 info = kmalloc(sizeof(*info) +
438 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
Bill Pembertonc3bf2e22010-05-05 15:27:34 -0400439 if (!info)
440 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700441
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700442 init_completion(&info->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700443
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800444 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700445
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800446 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
447 msg->child_relid = channel->offermsg.child_relid;
448 msg->gpadl = gpadl_handle;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700449
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800450 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800451 list_add_tail(&info->msglistentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800452 &vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800453 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc6977672011-01-26 12:12:08 -0800454 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700455 sizeof(struct vmbus_channel_gpadl_teardown));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700456
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800457 BUG_ON(ret != 0);
K. Y. Srinivasan40961de2011-06-06 15:50:05 -0700458 t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700459 BUG_ON(t == 0);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700460
Bill Pemberton454f18a2009-07-27 16:47:24 -0400461 /* Received a torndown response */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800462 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800463 list_del(&info->msglistentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800464 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700465
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700466 kfree(info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700467 return ret;
468}
Greg Kroah-Hartman18726d72010-10-21 08:39:59 -0700469EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700470
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700471static void vmbus_close_internal(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700472{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700473 struct vmbus_channel_close_channel *msg;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700474 int ret;
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700475 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700476
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700477 channel->state = CHANNEL_OPEN_STATE;
478 channel->sc_creation_callback = NULL;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400479 /* Stop callback and cancel the timer asap */
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700480 spin_lock_irqsave(&channel->inbound_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800481 channel->onchannel_callback = NULL;
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700482 spin_unlock_irqrestore(&channel->inbound_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700483
Bill Pemberton454f18a2009-07-27 16:47:24 -0400484 /* Send a closing message */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700485
K. Y. Srinivasane9a27a92011-06-06 15:49:59 -0700486 msg = &channel->close_msg.msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700487
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800488 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
489 msg->child_relid = channel->offermsg.child_relid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700490
Haiyang Zhangc6977672011-01-26 12:12:08 -0800491 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700492
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800493 BUG_ON(ret != 0);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400494 /* Tear down the gpadl for the channel's ring buffer */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800495 if (channel->ringbuffer_gpadlhandle)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700496 vmbus_teardown_gpadl(channel,
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800497 channel->ringbuffer_gpadlhandle);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700498
Bill Pemberton454f18a2009-07-27 16:47:24 -0400499 /* Cleanup the ring buffers for this channel */
K. Y. Srinivasan2dba6882011-05-10 07:55:22 -0700500 hv_ringbuffer_cleanup(&channel->outbound);
501 hv_ringbuffer_cleanup(&channel->inbound);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700502
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800503 free_pages((unsigned long)channel->ringbuffer_pages,
504 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700505
Hank Janssen3e7ee492009-07-13 16:02:34 -0700506
Hank Janssen3e7ee492009-07-13 16:02:34 -0700507}
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700508
509/*
510 * vmbus_close - Close the specified channel
511 */
512void vmbus_close(struct vmbus_channel *channel)
513{
514 struct list_head *cur, *tmp;
515 struct vmbus_channel *cur_channel;
516
517 if (channel->primary_channel != NULL) {
518 /*
519 * We will only close sub-channels when
520 * the primary is closed.
521 */
522 return;
523 }
524 /*
525 * Close all the sub-channels first and then close the
526 * primary channel.
527 */
528 list_for_each_safe(cur, tmp, &channel->sc_list) {
529 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
530 if (cur_channel->state != CHANNEL_OPENED_STATE)
531 continue;
532 vmbus_close_internal(cur_channel);
533 }
534 /*
535 * Now close the primary.
536 */
537 vmbus_close_internal(channel);
538}
Greg Kroah-Hartman70bfa302010-10-21 09:52:22 -0700539EXPORT_SYMBOL_GPL(vmbus_close);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700540
Hank Janssenc88c4e42010-05-04 15:55:05 -0700541/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700542 * vmbus_sendpacket() - Send the specified buffer on the given channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700543 * @channel: Pointer to vmbus_channel structure.
544 * @buffer: Pointer to the buffer you want to receive the data into.
545 * @bufferlen: Maximum size of what the the buffer will hold
546 * @requestid: Identifier of the request
547 * @type: Type of packet that is being send e.g. negotiate, time
Hank Janssenc88c4e42010-05-04 15:55:05 -0700548 * packet etc.
549 *
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700550 * Sends data in @buffer directly to hyper-v via the vmbus
Hank Janssenc88c4e42010-05-04 15:55:05 -0700551 * This will send the data unparsed to hyper-v.
552 *
553 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700554 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700555int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700556 u32 bufferlen, u64 requestid,
557 enum vmbus_packet_type type, u32 flags)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700558{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700559 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700560 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100561 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700562 struct scatterlist bufferlist[3];
563 u64 aligned_data = 0;
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700564 int ret;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800565 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700566
Hank Janssen3e7ee492009-07-13 16:02:34 -0700567
Bill Pemberton454f18a2009-07-27 16:47:24 -0400568 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800569 desc.type = type; /* VmbusPacketTypeDataInBand; */
570 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700571 /* in 8-bytes granularity */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800572 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
573 desc.len8 = (u16)(packetlen_aligned >> 3);
574 desc.trans_id = requestid;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700575
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700576 sg_init_table(bufferlist, 3);
577 sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
578 sg_set_buf(&bufferlist[1], buffer, bufferlen);
579 sg_set_buf(&bufferlist[2], &aligned_data,
580 packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700581
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800582 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700583
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800584 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700585 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700586
Hank Janssen3e7ee492009-07-13 16:02:34 -0700587 return ret;
588}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700589EXPORT_SYMBOL(vmbus_sendpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700590
Hank Janssen3e189512010-03-04 22:11:00 +0000591/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700592 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
Hank Janssen3e189512010-03-04 22:11:00 +0000593 * packets using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700594 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700595int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700596 struct hv_page_buffer pagebuffers[],
597 u32 pagecount, void *buffer, u32 bufferlen,
598 u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700599{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700600 int ret;
601 int i;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000602 struct vmbus_channel_packet_page_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700603 u32 descsize;
604 u32 packetlen;
605 u32 packetlen_aligned;
606 struct scatterlist bufferlist[3];
607 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800608 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700609
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700610 if (pagecount > MAX_PAGE_BUFFER_COUNT)
Bill Pemberton002b53e2010-05-05 15:27:39 -0400611 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700612
Hank Janssen3e7ee492009-07-13 16:02:34 -0700613
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700614 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000615 * Adjust the size down since vmbus_channel_packet_page_buffer is the
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700616 * largest size we support
617 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700618 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
619 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700620 sizeof(struct hv_page_buffer));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700621 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100622 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700623
Bill Pemberton454f18a2009-07-27 16:47:24 -0400624 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800625 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000626 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700627 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
628 desc.length8 = (u16)(packetlen_aligned >> 3);
629 desc.transactionid = requestid;
630 desc.rangecount = pagecount;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700631
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700632 for (i = 0; i < pagecount; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800633 desc.range[i].len = pagebuffers[i].len;
634 desc.range[i].offset = pagebuffers[i].offset;
635 desc.range[i].pfn = pagebuffers[i].pfn;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700636 }
637
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700638 sg_init_table(bufferlist, 3);
639 sg_set_buf(&bufferlist[0], &desc, descsize);
640 sg_set_buf(&bufferlist[1], buffer, bufferlen);
641 sg_set_buf(&bufferlist[2], &aligned_data,
642 packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700643
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800644 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700645
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800646 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700647 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700648
Hank Janssen3e7ee492009-07-13 16:02:34 -0700649 return ret;
650}
Greg Kroah-Hartman713efeb2010-10-21 09:29:54 -0700651EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700652
Hank Janssen3e189512010-03-04 22:11:00 +0000653/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700654 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
Hank Janssen3e189512010-03-04 22:11:00 +0000655 * using a GPADL Direct packet type.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700656 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700657int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700658 struct hv_multipage_buffer *multi_pagebuffer,
659 void *buffer, u32 bufferlen, u64 requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700660{
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700661 int ret;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000662 struct vmbus_channel_packet_multipage_buffer desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700663 u32 descsize;
664 u32 packetlen;
665 u32 packetlen_aligned;
666 struct scatterlist bufferlist[3];
667 u64 aligned_data = 0;
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800668 bool signal = false;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800669 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
670 multi_pagebuffer->len);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700671
Hank Janssen3e7ee492009-07-13 16:02:34 -0700672
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700673 if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
Bill Pemberton002b53e2010-05-05 15:27:39 -0400674 return -EINVAL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700675
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700676 /*
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000677 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700678 * the largest size we support
679 */
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700680 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
681 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700682 sizeof(u64));
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700683 packetlen = descsize + bufferlen;
Uwe Kleine-König73509682011-01-20 09:32:01 +0100684 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700685
Hank Janssen3e7ee492009-07-13 16:02:34 -0700686
Bill Pemberton454f18a2009-07-27 16:47:24 -0400687 /* Setup the descriptor */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800688 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000689 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700690 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
691 desc.length8 = (u16)(packetlen_aligned >> 3);
692 desc.transactionid = requestid;
Haiyang Zhang430a8e92010-09-20 21:07:51 +0000693 desc.rangecount = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700694
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800695 desc.range.len = multi_pagebuffer->len;
696 desc.range.offset = multi_pagebuffer->offset;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700697
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800698 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700699 pfncount * sizeof(u64));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700700
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700701 sg_init_table(bufferlist, 3);
702 sg_set_buf(&bufferlist[0], &desc, descsize);
703 sg_set_buf(&bufferlist[1], buffer, bufferlen);
704 sg_set_buf(&bufferlist[2], &aligned_data,
705 packetlen_aligned - packetlen);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700706
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800707 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700708
K. Y. Srinivasan98fa8cf2012-12-01 06:46:36 -0800709 if (ret == 0 && signal)
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700710 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700711
Hank Janssen3e7ee492009-07-13 16:02:34 -0700712 return ret;
713}
Greg Kroah-Hartman4cb106f2010-10-21 09:23:59 -0700714EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
Hank Janssenc88c4e42010-05-04 15:55:05 -0700715
716/**
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700717 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700718 * @channel: Pointer to vmbus_channel structure.
719 * @buffer: Pointer to the buffer you want to receive the data into.
720 * @bufferlen: Maximum size of what the the buffer will hold
721 * @buffer_actual_len: The actual size of the data after it was received
722 * @requestid: Identifier of the request
Hank Janssenc88c4e42010-05-04 15:55:05 -0700723 *
724 * Receives directly from the hyper-v vmbus and puts the data it received
725 * into Buffer. This will receive the data unparsed from hyper-v.
726 *
727 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700728 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700729int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700730 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700731{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700732 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700733 u32 packetlen;
734 u32 userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700735 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800736 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700737
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700738 *buffer_actual_len = 0;
739 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700740
Hank Janssen3e7ee492009-07-13 16:02:34 -0700741
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700742 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700743 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700744 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700745 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700746
Haiyang Zhang415f2282011-01-26 12:12:13 -0800747 packetlen = desc.len8 << 3;
748 userlen = packetlen - (desc.offset8 << 3);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700749
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700750 *buffer_actual_len = userlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700751
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700752 if (userlen > bufferlen) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700753
Hank Janssen0a466182011-03-29 13:58:47 -0700754 pr_err("Buffer too small - got %d needs %d\n",
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700755 bufferlen, userlen);
K. Y. Srinivasan926ae5262011-06-06 15:50:11 -0700756 return -ETOOSMALL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700757 }
758
Haiyang Zhang415f2282011-01-26 12:12:13 -0800759 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700760
Bill Pemberton454f18a2009-07-27 16:47:24 -0400761 /* Copy over the packet to the user buffer */
K. Y. Srinivasan38397c82011-05-10 07:55:25 -0700762 ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800763 (desc.offset8 << 3), &signal);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700764
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800765 if (signal)
766 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700767
Hank Janssen3e7ee492009-07-13 16:02:34 -0700768 return 0;
769}
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700770EXPORT_SYMBOL(vmbus_recvpacket);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700771
Hank Janssen3e189512010-03-04 22:11:00 +0000772/*
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700773 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700774 */
Haiyang Zhangfff41b22010-10-07 11:40:08 -0700775int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700776 u32 bufferlen, u32 *buffer_actual_len,
777 u64 *requestid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700778{
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700779 struct vmpacket_descriptor desc;
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700780 u32 packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700781 int ret;
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800782 bool signal = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700783
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700784 *buffer_actual_len = 0;
785 *requestid = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700786
Hank Janssen3e7ee492009-07-13 16:02:34 -0700787
K. Y. Srinivasana89186c2011-05-10 07:55:24 -0700788 ret = hv_ringbuffer_peek(&channel->inbound, &desc,
Greg Kroah-Hartmanf4266e32009-09-01 17:24:57 -0700789 sizeof(struct vmpacket_descriptor));
K. Y. Srinivasandad76bf2011-08-27 11:31:33 -0700790 if (ret != 0)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700791 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700792
Hank Janssen3e7ee492009-07-13 16:02:34 -0700793
Haiyang Zhang415f2282011-01-26 12:12:13 -0800794 packetlen = desc.len8 << 3;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700795
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700796 *buffer_actual_len = packetlen;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700797
Haiyang Zhang39d70a42010-09-30 10:52:13 -0700798 if (packetlen > bufferlen) {
Hank Janssen0a466182011-03-29 13:58:47 -0700799 pr_err("Buffer too small - needed %d bytes but "
800 "got space for only %d bytes\n",
801 packetlen, bufferlen);
K. Y. Srinivasan3d5cad92011-08-25 09:48:59 -0700802 return -ENOBUFS;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700803 }
804
Haiyang Zhang415f2282011-01-26 12:12:13 -0800805 *requestid = desc.trans_id;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700806
Bill Pemberton454f18a2009-07-27 16:47:24 -0400807 /* Copy over the entire packet to the user buffer */
K. Y. Srinivasanc2b8e522012-12-01 06:46:57 -0800808 ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
809 &signal);
810
811 if (signal)
812 vmbus_setevent(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700813
Dan Carpenter33b06932013-09-17 00:58:36 +0300814 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700815}
Greg Kroah-Hartmanadaee6b2010-10-21 09:09:23 -0700816EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);