blob: 9ffbfc575a0c1d14b451c9cc304d119dcd3c9069 [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-Hartmana0086dc2009-08-17 17:22:08 -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>
Bill Pemberton53af5452009-09-11 21:46:44 -040028#include <linux/list.h>
Hank Janssenc88c4e42010-05-04 15:55:05 -070029#include <linux/module.h>
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +000030#include <linux/completion.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070031#include <linux/hyperv.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070032
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070033#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070034
Greg Kroah-Hartman1d7e9072009-08-28 16:26:11 -070035struct vmbus_channel_message_table_entry {
K. Y. Srinivasancf9dcba2011-05-10 07:55:37 -070036 enum vmbus_channel_message_type message_type;
K. Y. Srinivasanfa90f1d2011-05-10 07:55:36 -070037 void (*message_handler)(struct vmbus_channel_message_header *msg);
Greg Kroah-Hartman1d7e9072009-08-28 16:26:11 -070038};
Hank Janssen3e7ee492009-07-13 16:02:34 -070039
Hank Janssenc88c4e42010-05-04 15:55:05 -070040
41/**
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -060042 * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
Hank Janssenc88c4e42010-05-04 15:55:05 -070043 * @icmsghdrp: Pointer to msg header structure
44 * @icmsg_negotiate: Pointer to negotiate message structure
45 * @buf: Raw buffer channel data
46 *
47 * @icmsghdrp is of type &struct icmsg_hdr.
48 * @negop is of type &struct icmsg_negotiate.
49 * Set up and fill in default negotiate response message. This response can
50 * come from both the vmbus driver and the hv_utils driver. The current api
51 * will respond properly to both Windows 2008 and Windows 2008-R2 operating
52 * systems.
53 *
54 * Mainly used by Hyper-V drivers.
55 */
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -060056void vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
57 struct icmsg_negotiate *negop, u8 *buf)
Hank Janssenc88c4e42010-05-04 15:55:05 -070058{
59 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
60 icmsghdrp->icmsgsize = 0x10;
61
62 negop = (struct icmsg_negotiate *)&buf[
63 sizeof(struct vmbuspipe_hdr) +
64 sizeof(struct icmsg_hdr)];
65
66 if (negop->icframe_vercnt == 2 &&
67 negop->icversion_data[1].major == 3) {
68 negop->icversion_data[0].major = 3;
69 negop->icversion_data[0].minor = 0;
70 negop->icversion_data[1].major = 3;
71 negop->icversion_data[1].minor = 0;
72 } else {
73 negop->icversion_data[0].major = 1;
74 negop->icversion_data[0].minor = 0;
75 negop->icversion_data[1].major = 1;
76 negop->icversion_data[1].minor = 0;
77 }
78
79 negop->icframe_vercnt = 1;
80 negop->icmsg_vercnt = 1;
81 }
82}
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -060083EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
Hank Janssenc88c4e42010-05-04 15:55:05 -070084
Hank Janssen3e189512010-03-04 22:11:00 +000085/*
Haiyang Zhange98cb272010-10-15 10:14:07 -070086 * alloc_channel - Allocate and initialize a vmbus channel object
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -070087 */
Greg Kroah-Hartman50fe56d2010-10-20 15:51:57 -070088static struct vmbus_channel *alloc_channel(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -070089{
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -070090 struct vmbus_channel *channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -070091
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -070092 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
Hank Janssen3e7ee492009-07-13 16:02:34 -070093 if (!channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -070094 return NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -070095
Greg Kroah-Hartman54411c42009-07-15 14:48:32 -070096 spin_lock_init(&channel->inbound_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070097
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -080098 channel->controlwq = create_workqueue("hv_vmbus_ctl");
99 if (!channel->controlwq) {
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700100 kfree(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700101 return NULL;
102 }
103
104 return channel;
105}
106
Hank Janssen3e189512010-03-04 22:11:00 +0000107/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700108 * release_hannel - Release the vmbus channel object itself
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700109 */
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200110static void release_channel(struct work_struct *work)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700111{
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200112 struct vmbus_channel *channel = container_of(work,
113 struct vmbus_channel,
114 work);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700115
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800116 destroy_workqueue(channel->controlwq);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700117
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700118 kfree(channel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700119}
120
Hank Janssen3e189512010-03-04 22:11:00 +0000121/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700122 * free_channel - Release the resources used by the vmbus channel object
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700123 */
Greg Kroah-Hartman9f3e28e2011-10-11 09:40:01 -0600124static void free_channel(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700125{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700126
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700127 /*
128 * We have to release the channel's workqueue/thread in the vmbus's
129 * workqueue/thread context
130 * ie we can't destroy ourselves.
131 */
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200132 INIT_WORK(&channel->work, release_channel);
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800133 queue_work(vmbus_connection.work_queue, &channel->work);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700134}
135
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000136
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000137
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200138/*
139 * vmbus_process_rescind_offer -
140 * Rescind the offer by initiating a device removal
141 */
142static void vmbus_process_rescind_offer(struct work_struct *work)
143{
144 struct vmbus_channel *channel = container_of(work,
145 struct vmbus_channel,
146 work);
147
K. Y. Srinivasan696453b2011-09-08 07:24:14 -0700148 vmbus_device_unregister(channel->device_obj);
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200149}
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000150
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800151void vmbus_free_channels(void)
152{
153 struct vmbus_channel *channel;
154
155 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
156 vmbus_device_unregister(channel->device_obj);
157 kfree(channel->device_obj);
158 free_channel(channel);
159 }
160}
161
Hank Janssen3e189512010-03-04 22:11:00 +0000162/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700163 * vmbus_process_offer - Process the offer by creating a channel/device
Hank Janssenc88c4e42010-05-04 15:55:05 -0700164 * associated with this offer
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700165 */
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200166static void vmbus_process_offer(struct work_struct *work)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700167{
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200168 struct vmbus_channel *newchannel = container_of(work,
169 struct vmbus_channel,
170 work);
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700171 struct vmbus_channel *channel;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700172 bool fnew = true;
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700173 int ret;
Greg Kroah-Hartman0f5e44c2009-07-15 14:57:16 -0700174 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700175
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200176 /* The next possible work is rescind handling */
177 INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
178
Bill Pemberton454f18a2009-07-27 16:47:24 -0400179 /* Make sure this is a new offer */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800180 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700181
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800182 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700183 if (!uuid_le_cmp(channel->offermsg.offer.if_type,
184 newchannel->offermsg.offer.if_type) &&
185 !uuid_le_cmp(channel->offermsg.offer.if_instance,
186 newchannel->offermsg.offer.if_instance)) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700187 fnew = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700188 break;
189 }
190 }
191
Haiyang Zhang188963e2010-10-15 10:14:06 -0700192 if (fnew)
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800193 list_add_tail(&newchannel->listentry,
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800194 &vmbus_connection.chn_list);
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700195
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800196 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700197
Haiyang Zhang188963e2010-10-15 10:14:06 -0700198 if (!fnew) {
Haiyang Zhange98cb272010-10-15 10:14:07 -0700199 free_channel(newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700200 return;
201 }
202
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700203 /*
204 * Start the process of binding this offer to the driver
205 * We need to set the DeviceObject field before calling
Haiyang Zhang646f1ea2011-01-26 12:12:10 -0800206 * vmbus_child_dev_add()
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700207 */
K. Y. Srinivasanf2c73012011-09-08 07:24:12 -0700208 newchannel->device_obj = vmbus_device_create(
Haiyang Zhang767dff62011-01-26 12:12:12 -0800209 &newchannel->offermsg.offer.if_type,
210 &newchannel->offermsg.offer.if_instance,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700211 newchannel);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700212
Bill Pemberton454f18a2009-07-27 16:47:24 -0400213 /*
214 * Add the new device to the bus. This will kick off device-driver
215 * binding which eventually invokes the device driver's AddDevice()
216 * method.
217 */
K. Y. Srinivasan227942812011-09-08 07:24:13 -0700218 ret = vmbus_device_register(newchannel->device_obj);
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700219 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -0700220 pr_err("unable to add child device object (relid %d)\n",
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800221 newchannel->offermsg.child_relid);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700222
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800223 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800224 list_del(&newchannel->listentry);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800225 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
K. Y. Srinivasan8b8ee672011-12-12 09:29:16 -0800226 kfree(newchannel->device_obj);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700227
Haiyang Zhange98cb272010-10-15 10:14:07 -0700228 free_channel(newchannel);
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700229 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400230 /*
231 * This state is used to indicate a successful open
232 * so that when we do close the channel normally, we
233 * can cleanup properly
234 */
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800235 newchannel->state = CHANNEL_OPEN_STATE;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700236 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700237}
238
Hank Janssen3e189512010-03-04 22:11:00 +0000239/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700240 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700241 *
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700242 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700243static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700244{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700245 struct vmbus_channel_offer_channel *offer;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700246 struct vmbus_channel *newchannel;
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700247 uuid_le *guidtype;
248 uuid_le *guidinstance;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700249
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700250 offer = (struct vmbus_channel_offer_channel *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700251
Haiyang Zhang767dff62011-01-26 12:12:12 -0800252 guidtype = &offer->offer.if_type;
253 guidinstance = &offer->offer.if_instance;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700254
Bill Pemberton454f18a2009-07-27 16:47:24 -0400255 /* Allocate the channel object and save this offer. */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700256 newchannel = alloc_channel();
Haiyang Zhang188963e2010-10-15 10:14:06 -0700257 if (!newchannel) {
Hank Janssen0a466182011-03-29 13:58:47 -0700258 pr_err("Unable to allocate channel object\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700259 return;
260 }
261
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800262 memcpy(&newchannel->offermsg, offer,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700263 sizeof(struct vmbus_channel_offer_channel));
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800264 newchannel->monitor_grp = (u8)offer->monitorid / 32;
265 newchannel->monitor_bit = (u8)offer->monitorid % 32;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700266
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200267 INIT_WORK(&newchannel->work, vmbus_process_offer);
268 queue_work(newchannel->controlwq, &newchannel->work);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700269}
270
Hank Janssen3e189512010-03-04 22:11:00 +0000271/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700272 * vmbus_onoffer_rescind - Rescind offer handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700273 *
274 * We queue a work item to process this offer synchronously
275 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700276static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700277{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700278 struct vmbus_channel_rescind_offer *rescind;
Greg Kroah-Hartmanaded71652009-08-18 15:21:19 -0700279 struct vmbus_channel *channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700280
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700281 rescind = (struct vmbus_channel_rescind_offer *)hdr;
Haiyang Zhangc6977672011-01-26 12:12:08 -0800282 channel = relid2channel(rescind->child_relid);
Hank Janssen98e08702011-03-29 13:58:44 -0700283
284 if (channel == NULL)
285 /* Just return here, no channel found */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700286 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700287
Timo Teräs4b2f9ab2010-12-15 20:48:09 +0200288 /* work is initialized for vmbus_process_rescind_offer() from
289 * vmbus_process_offer() where the channel got created */
290 queue_work(channel->controlwq, &channel->work);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700291}
292
Hank Janssen3e189512010-03-04 22:11:00 +0000293/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700294 * vmbus_onoffers_delivered -
295 * This is invoked when all offers have been delivered.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700296 *
297 * Nothing to do here.
298 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700299static void vmbus_onoffers_delivered(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700300 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700301{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700302}
303
Hank Janssen3e189512010-03-04 22:11:00 +0000304/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700305 * vmbus_onopen_result - Open result handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700306 *
307 * This is invoked when we received a response to our channel open request.
308 * Find the matching request, copy the response and signal the requesting
309 * thread.
310 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700311static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700312{
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700313 struct vmbus_channel_open_result *result;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700314 struct vmbus_channel_msginfo *msginfo;
315 struct vmbus_channel_message_header *requestheader;
316 struct vmbus_channel_open_channel *openmsg;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700317 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700318
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700319 result = (struct vmbus_channel_open_result *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700320
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700321 /*
322 * Find the open msg, copy the result and signal/unblock the wait event
323 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800324 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700325
Hank Janssenebb61e52011-02-18 12:39:57 -0800326 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
327 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700328 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800329 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700330
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800331 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700332 openmsg =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800333 (struct vmbus_channel_open_channel *)msginfo->msg;
334 if (openmsg->child_relid == result->child_relid &&
335 openmsg->openid == result->openid) {
336 memcpy(&msginfo->response.open_result,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700337 result,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700338 sizeof(
339 struct vmbus_channel_open_result));
340 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700341 break;
342 }
343 }
344 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800345 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700346}
347
Hank Janssen3e189512010-03-04 22:11:00 +0000348/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700349 * vmbus_ongpadl_created - GPADL created handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700350 *
351 * This is invoked when we received a response to our gpadl create request.
352 * Find the matching request, copy the response and signal the requesting
353 * thread.
354 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700355static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700356{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700357 struct vmbus_channel_gpadl_created *gpadlcreated;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700358 struct vmbus_channel_msginfo *msginfo;
359 struct vmbus_channel_message_header *requestheader;
360 struct vmbus_channel_gpadl_header *gpadlheader;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700361 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700362
Haiyang Zhang188963e2010-10-15 10:14:06 -0700363 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700364
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700365 /*
366 * Find the establish msg, copy the result and signal/unblock the wait
367 * event
368 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800369 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700370
Hank Janssenebb61e52011-02-18 12:39:57 -0800371 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
372 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700373 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800374 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700375
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800376 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700377 gpadlheader =
378 (struct vmbus_channel_gpadl_header *)requestheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700379
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800380 if ((gpadlcreated->child_relid ==
381 gpadlheader->child_relid) &&
382 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
383 memcpy(&msginfo->response.gpadl_created,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700384 gpadlcreated,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700385 sizeof(
386 struct vmbus_channel_gpadl_created));
387 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700388 break;
389 }
390 }
391 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800392 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700393}
394
Hank Janssen3e189512010-03-04 22:11:00 +0000395/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700396 * vmbus_ongpadl_torndown - GPADL torndown handler.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700397 *
398 * This is invoked when we received a response to our gpadl teardown request.
399 * Find the matching request, copy the response and signal the requesting
400 * thread.
401 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700402static void vmbus_ongpadl_torndown(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700403 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700404{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700405 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700406 struct vmbus_channel_msginfo *msginfo;
407 struct vmbus_channel_message_header *requestheader;
408 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700409 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700410
Haiyang Zhang188963e2010-10-15 10:14:06 -0700411 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700412
413 /*
414 * Find the open msg, copy the result and signal/unblock the wait event
415 */
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800416 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700417
Hank Janssenebb61e52011-02-18 12:39:57 -0800418 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
419 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700420 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800421 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700422
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800423 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700424 gpadl_teardown =
425 (struct vmbus_channel_gpadl_teardown *)requestheader;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700426
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800427 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
428 memcpy(&msginfo->response.gpadl_torndown,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700429 gpadl_torndown,
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700430 sizeof(
431 struct vmbus_channel_gpadl_torndown));
432 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700433 break;
434 }
435 }
436 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800437 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700438}
439
Hank Janssen3e189512010-03-04 22:11:00 +0000440/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700441 * vmbus_onversion_response - Version response handler
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700442 *
443 * This is invoked when we received a response to our initiate contact request.
444 * Find the matching request, copy the response and signal the requesting
445 * thread.
446 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700447static void vmbus_onversion_response(
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700448 struct vmbus_channel_message_header *hdr)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700449{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700450 struct vmbus_channel_msginfo *msginfo;
451 struct vmbus_channel_message_header *requestheader;
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700452 struct vmbus_channel_initiate_contact *initiate;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700453 struct vmbus_channel_version_response *version_response;
Greg Kroah-Hartmandd0813b2009-07-15 14:56:45 -0700454 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700455
Haiyang Zhang188963e2010-10-15 10:14:06 -0700456 version_response = (struct vmbus_channel_version_response *)hdr;
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800457 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700458
Hank Janssenebb61e52011-02-18 12:39:57 -0800459 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
460 msglistentry) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700461 requestheader =
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800462 (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700463
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800464 if (requestheader->msgtype ==
465 CHANNELMSG_INITIATE_CONTACT) {
Haiyang Zhang188963e2010-10-15 10:14:06 -0700466 initiate =
467 (struct vmbus_channel_initiate_contact *)requestheader;
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800468 memcpy(&msginfo->response.version_response,
Haiyang Zhang188963e2010-10-15 10:14:06 -0700469 version_response,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700470 sizeof(struct vmbus_channel_version_response));
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700471 complete(&msginfo->waitevent);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700472 }
473 }
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800474 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700475}
476
Greg Kroah-Hartmanc8212f02009-08-31 21:51:50 -0700477/* Channel message dispatch table */
478static struct vmbus_channel_message_table_entry
K. Y. Srinivasanb7c6b022011-05-10 07:55:38 -0700479 channel_message_table[CHANNELMSG_COUNT] = {
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800480 {CHANNELMSG_INVALID, NULL},
481 {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer},
482 {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind},
483 {CHANNELMSG_REQUESTOFFERS, NULL},
484 {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered},
485 {CHANNELMSG_OPENCHANNEL, NULL},
486 {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result},
487 {CHANNELMSG_CLOSECHANNEL, NULL},
488 {CHANNELMSG_GPADL_HEADER, NULL},
489 {CHANNELMSG_GPADL_BODY, NULL},
490 {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created},
491 {CHANNELMSG_GPADL_TEARDOWN, NULL},
492 {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown},
493 {CHANNELMSG_RELID_RELEASED, NULL},
494 {CHANNELMSG_INITIATE_CONTACT, NULL},
495 {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response},
496 {CHANNELMSG_UNLOAD, NULL},
Greg Kroah-Hartmanc8212f02009-08-31 21:51:50 -0700497};
498
Hank Janssen3e189512010-03-04 22:11:00 +0000499/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700500 * vmbus_onmessage - Handler for channel protocol messages.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700501 *
502 * This is invoked in the vmbus worker thread context.
503 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700504void vmbus_onmessage(void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700505{
Haiyang Zhang188963e2010-10-15 10:14:06 -0700506 struct hv_message *msg = context;
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700507 struct vmbus_channel_message_header *hdr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700508 int size;
509
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800510 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
511 size = msg->header.payload_size;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700512
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800513 if (hdr->msgtype >= CHANNELMSG_COUNT) {
Hank Janssen0a466182011-03-29 13:58:47 -0700514 pr_err("Received invalid channel message type %d size %d\n",
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800515 hdr->msgtype, size);
Greg Kroah-Hartman04f50c42009-07-16 12:35:37 -0700516 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800517 (unsigned char *)msg->u.payload, size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700518 return;
519 }
520
K. Y. Srinivasanb7c6b022011-05-10 07:55:38 -0700521 if (channel_message_table[hdr->msgtype].message_handler)
522 channel_message_table[hdr->msgtype].message_handler(hdr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700523 else
Hank Janssen0a466182011-03-29 13:58:47 -0700524 pr_err("Unhandled channel message type %d\n", hdr->msgtype);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700525}
526
Hank Janssen3e189512010-03-04 22:11:00 +0000527/*
Haiyang Zhange98cb272010-10-15 10:14:07 -0700528 * vmbus_request_offers - Send a request to get all our pending offers.
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700529 */
Haiyang Zhange98cb272010-10-15 10:14:07 -0700530int vmbus_request_offers(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700531{
Greg Kroah-Hartman82250212009-08-26 15:16:04 -0700532 struct vmbus_channel_message_header *msg;
Haiyang Zhang188963e2010-10-15 10:14:06 -0700533 struct vmbus_channel_msginfo *msginfo;
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700534 int ret, t;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700535
Haiyang Zhang188963e2010-10-15 10:14:06 -0700536 msginfo = kmalloc(sizeof(*msginfo) +
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700537 sizeof(struct vmbus_channel_message_header),
538 GFP_KERNEL);
Haiyang Zhang188963e2010-10-15 10:14:06 -0700539 if (!msginfo)
Bill Pemberton75910f22010-05-05 15:27:31 -0400540 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700541
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700542 init_completion(&msginfo->waitevent);
Bill Pemberton75910f22010-05-05 15:27:31 -0400543
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800544 msg = (struct vmbus_channel_message_header *)msginfo->msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700545
Haiyang Zhangc50f7fb2010-11-08 14:04:38 -0800546 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700547
Hank Janssen3e7ee492009-07-13 16:02:34 -0700548
Haiyang Zhangc6977672011-01-26 12:12:08 -0800549 ret = vmbus_post_msg(msg,
Greg Kroah-Hartmanbd60c332009-08-31 21:47:21 -0700550 sizeof(struct vmbus_channel_message_header));
551 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -0700552 pr_err("Unable to request offers - %d\n", ret);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700553
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800554 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700555 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700556
K. Y. Srinivasan2dfde962011-06-16 13:16:34 -0700557 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
K. Y. Srinivasan9568a192011-05-10 07:55:39 -0700558 if (t == 0) {
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800559 ret = -ETIMEDOUT;
560 goto cleanup;
561 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700562
563
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800564
565cleanup:
Ilia Mirkindd9b15d2011-03-13 00:29:00 -0500566 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700567
Hank Janssen3e7ee492009-07-13 16:02:34 -0700568 return ret;
569}
570
Bill Pemberton454f18a2009-07-27 16:47:24 -0400571/* eof */